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 query(){
    return query(Task.class);
}
To find all Tasks with the query you just need to add the following findAll Method:
public static List 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

Blog url changed to https

I just changed the url of this blog to https://jensjaeger.com. TLS encryption is now the default for all request to this page. It might be possible that some image links on some articles are hard coded http. If you find such an error it would be nice if you leave me comment so i can

Read More

Format date and time in java with prettytime

Prettytime is a nice java library to format a java Date()s in a nice humanized ago format, like: moments ago 2 minutes ago 13 hours ago 7 months ago 2 years ago Prettytime is localized in over 30 languages. It’s super simple to use Add the dependency to your maven pom: org.ocpsoft.prettytime prettytime 3.2.7.Final or

Read More