Demo of using closures to implement LINQ-style queries in Java7

Ming-Yee Iu mingyeeiu+lambda at gmail.com
Tue Sep 7 14:02:32 PDT 2010


I made a small demo of how closures in Java finally enable us to write
LINQ-style (aka functional-style aka safe-query-object-style aka
Kliesli-style etc) database queries. The system is actually all Java6
code, but Java7's syntactic sugar for translating functions into
classes makes the queries short enough to be practical.

      http://documents.epfl.ch/users/i/iu/iu/www/demo.zip

The demo lets you write queries in Java like this:

      // Join using N:1 navigational links
      System.out.println("Sales made to Bob");
      result = em.allSale()
         .where(#(s){s.getPurchaser().getName().equals("Bob")});

The code will then automatically be rewritten at runtime into the
equivalent SQL query:

      SELECT A.SaleId AS COL1, A.Date AS COL2, A.CustomerId AS COL3
      FROM Sales AS A, Customers AS B
      WHERE ((A.CustomerId) = (B.CustomerId)) AND (((B.Name) = ('Bob')))

So once JDK7 gets released, the J2EE community should be able to
quickly make a query system that leapfrogs ahead of Rails and is
competitive with LINQ. I think this makes a compelling use-case for
closures in Java.


More information about the lambda-dev mailing list