1
votes

Let's assume that I have 2 entities: question and user and I want to return object that have questions and user information. How can I ask Ebean to make join and bind result to new class? (My intent that there will be 1 select join query to db, instead of fetching Questions and then for each issue query to bind user info to them).

For example:

@Entity
class User extends Model {
    String name;

    @ManyToOne
    @LazyLoad
    List<Questions> questions;
}

@Entity
class Question extends Model {
   String question;

   @OneToMany
   @LazyLoad
   User user;
}

@????
class QuestionUserResult {
   String question;
   String userName;
}
Ebean.find(QuestionUserResult.class) ????? // Intent there to have "select * from question inner join user on ...... "

Most examples on the web dealing with filtering. I do not care in this case about where condition.

Thanks for answers.

1

1 Answers

1
votes

I think what you want is this (I'm assuming a id property of type Long in this example):

import com.avaje.ebean.Model.Find;
import com.avaje.ebean.Model.Finder;

Find<Long, Question> finder = new Finder<Long,Question>(Question.class);
List<Question> result = finder.fetch("user.name").findList();

This will fetch all Question instances and make a join with the UserTable, but only retrieving the name of the user. Ebean has this notion of [partial objects] that makes it easy to just fetch what you want from them 1