December 29, 2013

A Ebean like finder for JPA

1  comments

After the decision to go for JPA instead of Ebean with a Play Framework project, the first problem to solve was a replacement for the ebean finder.

The project team started with these simple requirements:

  • Jpa based
  • Easy to use
  • Can return a finder object, to add restrictions outside of the model (Used for generic components like the sort function of a data table view)

After some prototyping with different implementations, we found this gist and decided to start from there with Hibernate Criteria.

You can find the results in the query package and the generic model of my Play4Jpa project. The GenericModel doesn’t contain a generated long based @Id. You should only extend from GenericModel when you use combined keys. If you need a long based @Id you can extend from Model.

To get a query object from a Object that extends Model. You just have to add the method:

public static Query< Task> query(){
    return query(Task.class);
}

To find all Tasks with the query you just need to add the following findAll Method:

public static List< Task> findAll(){
    return Task.query().findList();
}

Here is a example of a simple where condition:

public static Task findByName(String name){
    return Task.query().eq("name", name).findUnique();
}

Tags

Finder, Hibernate, JPA, Play Framework, Play4JPA, Query, sql


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

Information about Data protection

This site uses Akismet to reduce spam. Learn how your comment data is processed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}