December 31, 2013

Join example for JPA finder

0  comments

The JPA finder for play 2 works with joins as well. Imagine you have a Task model with a creator:
@Entity(name = "tasks")
public class Task extends Model {
    @ManyToOne
    public User creator;
}
referenced to a User model with a name:
@Entity(name = "users")
public class User extends Model {
    public String name;
}
You can write a method to get all Task for a creator username by joining on creator and access the user fields with the creator.field syntax:
public static List findByCreatorName(String creatorName){
   return Task.query().join("creator").eq("creator.name", creatorName).findList();
}
The source code for this example with unit tests is pushed to Play4Jpa

Tags

Java, join, JPA, Play Framework, Play4JPA


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