40

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?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Milhous
  • 14,473
  • 16
  • 63
  • 82
  • 3
    For LINQ to objects, check this one: github.com/nicholas22/jpropel-light, real example: new String[] { "james", "john", "john", "eddie" }.where(startsWith("j")).toList().distinct(); – NT_ Oct 08 '11 at 10:17
  • Scala LINQ: https://github.com/nicholas22/propelS – Scooterville Jun 09 '12 at 21:28

8 Answers8

63

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • what troubles are the java community experiencing with closures? – Claudiu Dec 07 '08 at 08:37
  • 3
    @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 Dec 07 '08 at 08:44
  • Where is an example of Projecting in the google collections api? – Nathan Feger Mar 16 '10 at 17:12
  • 1
    @Nathan: See Iterables.transform. – Jon Skeet Mar 16 '10 at 17:48
  • 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 Mar 16 '10 at 18:15
  • Now Java has Lambdas, and JaQue (https://github.com/TrigerSoft/jaque) can produce Expression Trees out of them. So language integration is not a problem any more. – Konstantin Triger Sep 27 '14 at 21:43
  • @KonstantinTriger: It's good to see that Java is catching up with where C# was so long ago - really, it is. I still prefer *compiler* support for expression trees to having a separate library which requires an implementation-specific system property to be set, and files to be generated and parsed at execution time... – Jon Skeet Sep 28 '14 at 08:01
14

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


Actually, scala standart collections provide API that works like LINQ-for-Objects in some sense. Here is the example:

List("Paris","Berlin","London","Tokyo")
  .filter(c => c.endsWith("n"))
  .map(c => c.length) 
// result would be length of the words that ends 
// with "n" letter ("Berlin" and "London").

Don't be scared of new-line-dot syntax: you can write code in plain old style:

Array(1,2,3,4,5,6).map(x => x*x)

And there is a number of projects that provide close to LINQ-to-SQL syntax. For example, snippet taken from Squeryll:

import Library._
using(session) { 
  books.insert(new Author(1, "Michel","Folco"))            
  val a = from(authors)(a=> where(a.lastName === "Folco") select(a)) 
}
// but note that there is more code behind this example
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Stephen
  • 19,488
  • 10
  • 62
  • 83
  • Seems like not yet, but here's hoping. Scala has done great feats before, like integrating XML into the language. And I hope it gets better :) – Hendy Irawan Dec 28 '10 at 12:01
8

For a more general approach to the issue, consider using 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
  • 21,824
  • 5
  • 78
  • 111
4

Java LINQ to SQL implementation. Provides full language integration and larger feature set compared to .NET LINQ.

Konstantin Triger
  • 1,576
  • 14
  • 11
1

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

yrcjaya
  • 413
  • 4
  • 8
0

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
  • 101
  • 1
  • 2
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ajlopez
  • 106
  • 2
0

Siena project seems good.

Fırat Küçük
  • 5,613
  • 2
  • 50
  • 53