InfoQ

News

Two Type-Safe Criteria API Proposals for JPA 2.0

Posted by Charles Humble on Jan 27, 2009

Community
Java
Topics
Data Access
Tags
JPA

Introduced two years ago as part of Java EE 5, the Java Persistence API provides a POJO persistence model for object-relational mapping. It was developed by the EJB 3.0 software Expert Group as part of JSR-220.  

 Persistence consists of three areas:

  1. The API, defined in the javax.persistence package.
  2. The Java Persistence Query Language (JPQL).
  3. Object/relational metadata.

Whilst the JPQL significantly improved working with persistent Java objects, JPQL queries are still specified as strings. Thus whilst the queries operate over strongly-typed Java objects, they themselves are weakly typed.  Building up queries in this way can be error prone, and requires specific IDE support for validation, auto completion and refactoring.

JPA 2.0, which is being developed under JSR-317 for inclusion in Java EE 6, aims to address this through the introduction of a new criteria API that offers a non-string-based approach for building queries.  Expert Group lead Linda DeMichiel has published a blog entry describing the current draft of the API:

"Loosely speaking, a QueryDefinition object can be thought of as a set of nodes corresponding to the semantic constructs of the query: 
  • Domain objects, which correspond to the range variables and other identification variables of the JPQL FROM clause
  • Where clause predicates, which comprise one or more conditional expression objects
  • Select clauses, which comprise one or more "select item" objects
  • Order-by and group-by items
  • Subqueries

and so on..."

Whilst the proposal as it stands appears to be a good step forward from the existing JPA mechanism a number of people, amongst them Gavin King, have argued that the type safety of it could and should be further improved.  King, whose Hibernate O/R tool was amongst those that pioneered the use of a type-safe criteria API and was a significant influence on EJB3, has now submitted his own proposal to the Expert Group. His proposal exploits the javax.annotation.Processor introduced in Java 6 to allow a compiler plugin to build a metamodel type for each persistent class in the application.  King has written two blogs posts (one, two) which describe his approach in more detail and he and his team are currently working on a prototype annotation processor for use with javac.

The Expert Group is looking at the King proposal seriously and is considering it as a replacement for the current review draft.  DeMichiel told us:

"Discussion has largely focused on ensuring that this API leads to a better experience for developers, both for static queries (where the type-safe aspects should certainly shine), and for dynamic queries.  We are also looking at the metamodel generation aspects."

She added that the Expert Group is very keen to hear any feedback from the development community.  Please feed any comments in to jsr-317-pdr-feedback at sun dot com.

16 comments

Watch Thread Reply

nice ..but by serge boulay Posted Jan 27, 2009 8:06 PM
Re: nice ..but by Gavin King Posted Jan 28, 2009 6:09 AM
Re: nice ..but by serge boulay Posted Jan 28, 2009 10:26 AM
too complicated by Thomas Mueller Posted Jan 28, 2009 12:07 AM
Re: too complicated by Gavin King Posted Jan 28, 2009 6:05 AM
Re: too complicated by Thomas Mueller Posted Jan 28, 2009 8:07 AM
Re: too complicated by Gavin King Posted Jan 28, 2009 9:06 AM
Re: too complicated by Gavin King Posted Jan 28, 2009 9:12 AM
Re: too complicated by Rubem Azenha Posted Jan 29, 2009 7:42 AM
Re: too complicated by Rubem Azenha Posted Jan 29, 2009 7:43 AM
Re: too complicated by William H Posted Jan 30, 2009 4:30 AM
Re: too complicated by Emmanuel Bernard Posted Jan 29, 2009 11:01 AM
Re: too complicated by William H Posted Jan 28, 2009 6:36 AM
Re: too complicated by Thomas Mueller Posted Jan 28, 2009 8:10 AM
Cayenne experience by Andrus Adamchik Posted Jan 29, 2009 3:34 AM
Re: Cayenne experience by Andrus Adamchik Posted Jan 29, 2009 3:47 AM
  1. Back to top

    nice ..but

    Jan 27, 2009 8:06 PM by serge boulay

    far better than strings but the code generation is really just a work around for language features that should allow for easy creation of domain specific languages. Something comparable to linq can only be accomplished with some moderate language changes.

  2. Back to top

    too complicated

    Jan 28, 2009 12:07 AM by Thomas Mueller

    Very verbose. Code generation... I don't like it. It's probably too early to standardize this part.

    I wonder if they considered technology like JaQu, LIQUidFORM, or Querydsl. Example (JaQu):

    Product p = new Product();
    List<Product> soldOutProducts =
    db.from(p).where(p.unitsInStock).is(0).select();

  3. Back to top

    Re: too complicated

    Jan 28, 2009 6:05 AM by Gavin King

    Very verbose.


    Yes, both JPA criteria API proposals are significantly more verbose than the fluent style of API you exemplify, and at first Emmanuel and I were *really* concerned about this and were advocating for the fluent style.

    However, it turns out that some of the more complex cases just don't map as elegantly to the fluent style of API, and so the JPA group made the decision to go with the current approach. I'm still not 100% happy, but now that I'm used to it, I don't think it's a really bg problem. There is definitely more typing involved, however.

    Code generation... I don't like it.


    That's only because youve never used a Java6 Processor :-) It's not really "code generation" in the traditional sense at all. It's an extension to the type system that is built into the compiler. This is one of those things like annotations that are scary until you use them :-)


    I wonder if they considered technology like JaQu, LIQUidFORM, or Querydsl.


    Yes, I explain why these solutions don't work for us here:

    in.relation.to/Bloggers/Java6CompilerPluginsAnd...

    It's a nice idea, but it simply breaks down as soon as you want to write queries around anything other than a public attribute. Doesn't work with common JPA patterns.

  4. Back to top

    Re: nice ..but

    Jan 28, 2009 6:09 AM by Gavin King

    far better than strings but the code generation is really just a work around for language features that should allow for easy creation of domain specific languages


    Yes, of course Java should have method and attribute literals. It's absurd that this was left out of the language.

    But you can forget about getting them anywhere in the near future - let alone something like LINQ!

    Actually I don't personally think LINQ is the right direction anyway. Better to have some more general-purpose declarative constructs in the language, that can be used to solve other problems beside "querying".

  5. Back to top

    Re: too complicated

    Jan 28, 2009 6:36 AM by William H

    Gavin's blog just mention LIQUidFORM:


    "Unfortunately that particular approach forces you to represent every persistent attribute as a public getter method, which is not a restriction that is acceptable in the JPA specification."

    So I guess they will have looked at least some of them
    I’ve not looked at JaQu before – it looks very like writing SQL to me. What advantages does it offer over using something like raw JDBC and the Apache Dynabeans libraries?

  6. Back to top

    Re: too complicated

    Jan 28, 2009 8:07 AM by Thomas Mueller

    complex cases don't map elegantly


    But most cases are simple. I suggest to support both: simple conditions with fluent, complex expressions with your style.

    Code generation...


    Yes, the Processor solves that. I hope tools support will improve.

    queries around [non] public attribute


    In my view that's an edge case.

  7. Back to top

    Re: too complicated

    Jan 28, 2009 8:10 AM by Thomas Mueller

    What advantages does it offer over using something like raw JDBC and the Apache Dynabeans libraries?
    From the JaQu docs: auto-complete, type checking, protects against SQL injection, smaller than Hibernate, no XML, no annotations.

  8. Back to top

    Re: too complicated

    Jan 28, 2009 9:06 AM by Gavin King

    In my view that's an edge case.


    Actually it's an extremely common usecase to have private persistent field exposed only by getters that don't necessarily map exactly 1-1 to the fields.

    That's how at least 30% - perhaps more - people use JPA.

  9. Back to top

    Re: too complicated

    Jan 28, 2009 9:12 AM by Gavin King

    But most cases are simple. I suggest to support both: simple conditions with fluent, complex expressions with your style.


    Earlier today I sent an email to that effect to the JPA list. I'm not sure if it will go anywhere.

  10. Back to top

    Re: nice ..but

    Jan 28, 2009 10:26 AM by serge boulay

    Joe Darcy is accepting language proposals for small changes to the jdk. I don't know if "method and attribute literals" are considered small but it would be nice to see something like this incorporated into the JDK. I know that FCM (closures proposal) had something like this - I suspect BGGA did also.

  11. Back to top

    Cayenne experience

    Jan 29, 2009 3:34 AM by Andrus Adamchik

    From Cayenne ORM experience that had typesafe queries from day one, and recently added type unsafe JPQL, the former, is a user choice over JPQL in 95% of cases, even though Cayenne SelectQuery is missing some of the capabilities of JPQL. So yeah, great to see type safe queries coming to JPA as well - that's the way to go.

  12. Back to top

    Re: Cayenne experience

    Jan 29, 2009 3:47 AM by Andrus Adamchik

    Just to clarify a bit, Cayenne "typesafe queries" are about building a query as an object made of parts vs. using a String. Type-safe criteria piece is still coming.

  13. Back to top

    Re: too complicated

    Jan 29, 2009 7:42 AM by Rubem Azenha

    I got a flashback when I was a "standard J2EE" developer, using tools like XDoclet and JBuilder for generating stubs, home, remote interfaces, local interfaces...

  14. Back to top

    Re: too complicated

    Jan 29, 2009 7:43 AM by Rubem Azenha

    I got a flashback *from* when ...

  15. Back to top

    Re: too complicated

    Jan 29, 2009 11:01 AM by Emmanuel Bernard

    About code generation flashback. I would like to emphasize what Gavin said. There is NO build time step to generate the metamodel. Thanks to the annotation processor magic built inside javac itself, the generation is part of the compilation. you don't even change the whay you invoke javac, just being in the classpath make annotation processors being triggered.

  16. Back to top

    Re: too complicated

    Jan 30, 2009 4:30 AM by William H

    I got a flashback when ...

    I think I liked the first version more!

    Something I find myself wondering about is how well this works when I have some reasonably complex SQL to execute - particularly if I need to rely on vendor specific stuff (SUM, IF, FROM_DAYS, TO_DAYS etc.) or if I’m using a UDF in MySQL say. I do quite a bit of work with financial companies and I’ve always ended up reverting to raw JDBC because everything else I’ve tried seems kind of painful.

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.