Closest equivalent to SQLAlchemy for Java/Scala
Squeryl provides composability similar to what they talk about in "SQLALCHEMY'S PHILOSOPHY" on the libraries home page. You can query a query.
val query = from(table)(t => where(t.a === 1) select(t))
val composed = from(query)(q => where(q.b === 2) select(q))
It also shares a good portion of the design philosophy, primarily that when things get complicated the library should "get out of the way" and allow the developer to tune things themselves. While Squeryl does object mapping, I consider it more of a DSL than an ORM.
Some of the features it doesn't share from a quick glance at the SQLAlchemy feature list:
- Ability to use raw SQL - it's difficult to find anything in standard ANSI SQL that can't be expressed though.
- Inheritance mapping - some personal opinion here, but I think that when a library does this it often violates the "get out of the way" tenet.
Of course, Squeryl also offers what I consider a major feature that a Python library can't, which is compiler checked type safety of your queries.
One of the notable things about SQLAlchemy is that it makes tables first class objects. Thus the core API is really written around table objects, and the API therefore is essentially relational in nature. Thus at this level even if the API is OO, it is essentially reflecting RDBMS objects or functions such as Tables, Columns, Relationships, Joins, Aliases etc. At this level SQLAlchemy gives you essentially a OOSQL where SQL and Relational Databases are not given the second class treatment. Its also at this that SQLAlchemy really shines since this level of abstraction gives you an ability to step down to a bit of a "raw" relational level and thus gain enormous amounts of flexibility which I really haven't seen any other ORM offer. Interestingly some of the underlying capabilities that are required to model class inheritance in ORMs are implemented at this layer eg. Joined Table Inheritance http://docs.sqlalchemy.org/en/rel_0_7/orm/inheritance.html#joined-table-inheritance
The API that is more often used (at least lately) is the declarative API which is really a lot more OO and maps objects in the business domain to the objects I referred to above (in most cases transparently). Here's where the ORM functionality comes in and the API is a little bit more similar to other ORM APIs where one works with domain objects and these actions get translated into the underlying table actions directly.
To the best of my awareness, ORMs in Scala are still playing catch-up to what is easily available in Java (eg. inheritance) even as they offer other capabilities (eg. type safety, LINQ like constructs), even as they struggle with some serious issues like 22 column limitations. (I've read comments where few have wondered why anyone would need more than 22 columns, and at least in my experience there are situations I wouldn't call rare, where one needs a multiple of that).
ORMs in scala (even as they take on a different flavour from Java) I think are still catching up to whats required. With respect to SQLAlchemy, is there a close enough equivalent to it I've seen in Java or Scala? I haven't seen any.
EDIT: One thing I forgot to add, is that even if one uses the declarative API, SQLAlchemy still gives you direct access to the underlying objects. So if "class Foo" is mapped declaratively, Foo.__table__ is the table object that you can directly use if you so desire.