Would a LINQ for java be a useful tool? I have been working on a tool that will allow a Java object to map to a row in a database.
- Would this be useful for Java programmers?
- What features would be useful?
Would a LINQ for java be a useful tool? I have been working on a tool that will allow a Java object to map to a row in a database.
LINQ for Java would be lovely, but the problem is the language integration.
Java doesn't have anything as concise as lambda expressions, and they're one of the bedrocks of LINQ. I suppose they could layer the query expression support on top of normal Java without lambda expressions, by making the expansion create anonymous inner classes - but it would be pretty hideous. You'd also need expression trees if you wanted to do anything like LINQ to SQL.
Checked exceptions might get in the way, but we'd have to see. The equivalent of IQueryable would need to have some sort of general checked exception - or possibly it could be generic in both the element type and the exception type...
Anyway, this is all pie-in-the-sky - given the troubles the Java community is having with closures, I think it would be folly to expect anything like LINQ in Java itself earlier than about 2012. Of course, that's not to say it wouldn't be possible in a "Java-like" language. Groovy has certain useful aspects already, for instance.
For the library side, Hibernate already provides a "non-integrated" version of a lot of the features of LINQ to SQL. For LINQ to Objects, you should look at the Google Java Collections API - it's a lot of the same kind of thing (filtering, projecting etc). Without lambdas it's a lot fiddlier to use, of course - but it's still really, really handy. (I use the Google Collections code all the time at work, and I'd hate to go back to the "vanilla" Java collections.)
Hibernate uses HQL. You can do Object but only for Relational Databases
For a more general approach to the issue, consider using Querydsl : http://source.mysema.com/display/querydsl/Querydsl
It provides a LINQ-style syntax with support for JPA/Hibernate, JDO, SQL and Java Collection backends.
I am the maintainer of Querydsl, so this answer is biased.
A more C#-like solution is http://code.google.com/p/jaque. It has both: linq-to-object functionality and a provider model with API very similar to MS LINQ. A simple JPA (Hibernate) provider is implemented. After Java will get closures (http://blogs.sun.com/mr/entry/closures), it will be elegant as well.
An extension to Java which gives LINQ-to-objects capabilities is SBQL4J. It offers:
Baby steps:
A first approach: implement Java LINQ using strings for expressions, instead of lambda.
Write IQueryProviders
based on string expressions.
Then, pursue for add the string expressions directly in the language.
But first, I want a LINQ that works: typed linq in language, it's a nice thing, but the MAIN point is: have a way to write IQueryProviders
, then, write a provider for POJOs, a provider for Hibernate, a provider for SQL Server or Oracle, etc...