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< User> {
    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< Task> 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

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"}