The complex is combined using the Play Framework and Ebean

I use PlayFramework and I like it a lot. When I want to grab data from a table, for example, I have a user table, I use the following syntax:

 List<User> users = User.find.where().eq("email", email).findList(); 

My question is that when I get the user object, I have an id column. Using this id value, I can map other tables, and the id these tables can be compared with even more tables, so the basic concept of joining multiple tables. Is there any example or place that I can read where it describes how to implement this using the above syntax?

I tried to find myself and could not, only the way I can think about it at this stage is to use direct sql with prepared statements, which I would prefer not to do.

+7
source share
1 answer

ellou 'kalvish

Relationships between models establish common JPA annotations such as @OneToMany , @ManyToOne , @OneToOne , etc.

So, if you have a User.java model for a user table and a Question.java model for a Question user, you can join it using @OneToMany (One User has many Question s)

User

 @Entity public class User extends Model { @Id public Long id; public String email; @OneToMany public List<Question> questions; } 

Question

 @Entity public class Question extends Model { @Id public Long id; public String question; } 

When you select a user in the controller, Ebean will default to “connections” and also select all user questions:

 User user = User.find.where().eq("email", email).findUnique(); List<Question> usersQuestion = user.questions; 

By default, Ebean retrieves all the properties and relationships of objects, so you don't need to create subqueries. Of course, you can or should even select / select only the data that is currently required.

You will find a good reference guide (pdf) on the official Ebean documentation page , a general description of the relationship is available in Section 11.6.2 Relationships .

4.1.2 Query section 4.1.2 Query has an example (second) that demonstrates how to get a “partial” object using select() and fetch()

+16
source

All Articles