I’m using the PlayFramework and I’m really liking it. 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. With that id value I can map to other tables and the id‘s of those tables can be mapped to even more tables, so basic concept of joining across several tables. Is there any example or place I can read where it describes how to implement that with the above-like syntax?
I tried to find myself and couldn’t, only way I can think of it at this point is to use straight sql with prepared statements which I’d rather not do.
ellou’ kalvish
Relationshipsbetween models are set with common JPA annotations like@OneToMany,@ManyToOne,@OneToOne, etc.So if you have
User.javamodel for user table andQuestion.javamodel for user’s Question you can join them with@OneToMany(OneUserhas ManyQuestions)User
Question
When you’ll select a User in controller, Ebean will perform ‘joins’ by default and will fetch all user’s questions as well:
By default Ebean fetches all object’s properties and relations, so you don’t need to create subqueries. Of course you can or even should select/fetch only the data that is required at the moment.
At the official Ebean documentation page you’ll find quite good Reference guide (pdf), general description of relationship is available in section
11.6.2 Relationships.In section
4.1.2 Querythere is example (second) which demonstrates how to get “partial” object with use ofselect()andfetch()