Join example for JPA finder
This post is part of the archive. It was written years ago, so some of the tools, versions and links may be outdated.
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