tags:

views:

7070

answers:

10
+14  Q: 

LINQ for Java tool

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.

  1. Would this be useful for Java programmers?
  2. What features would be useful?
+33  A: 

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.)

Jon Skeet
what troubles are the java community experiencing with closures?
Claudiu
@Claudiu: Several proposals, none of them entirely satisfactory. Last I heard it sounded unlikely that it would even get into Java 7, due to there being so much discussion. This is a downside of Java being so democratic.
Jon Skeet
Where is an example of Projecting in the google collections api?
Nathan Feger
@Nathan: See Iterables.transform.
Jon Skeet
@Jon Skeet thanks, I figured it had to do with the transform method, I thought perhaps there might be some less manual approach, like a hibernate Projections kind of api.
Nathan Feger
+2  A: 

There is a Google Group exploring this idea:

http://groups.google.com/group/jlinq

Alex Miller
+1  A: 

Hibernate uses HQL. You can do Object but only for Relational Databases

yrcjaya
A: 

Take a look at Quaere

kamal.gs
this link does not work.
Nathan Feger
This works, though: http://quaere.codehaus.org/
catbert
+7  A: 

It's worth noting that Scala 2.8 is going to have LINQ support...

Stephen
+4  A: 

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.

Timo Westkämper
+4  A: 

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.

Konstantin Triger
A: 

An extension to Java which gives LINQ-to-objects capabilities is SBQL4J. It offers:

  • Type-safety in compile time
  • Powerful query engine with greater capabilities than LINQ
  • Compatibility with current JVMs (it uses preprocessing to translate queries to pure Java code)
  • Nice performance (no reflection is used at runtime)
  • Clear, precise semantics without needless, obscure syntactic sugar.
Emil Wcisło
A: 

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...

A: 

Siena project seems good.

Fırat KÜÇÜK