JPA/Ebean select 加入
JPA/Ebean select join
假设我有 2 个实体:问题和用户,我想 return 包含问题和用户信息的对象。我怎样才能让 Ebean 加入并将结果绑定到新的 class? (我的意图是将有 1 个 select 连接查询到数据库,而不是获取问题,然后针对每个问题查询将用户信息绑定到它们)。
例如:
@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 ...... "
网络上的大多数示例都与过滤有关。在这种情况下,我不关心 where 条件。
感谢您的回答。
我想你想要的是这个(在这个例子中我假设一个 id 属性 类型为 Long):
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();
这将获取所有 Question 实例并与 UserTable 进行连接,但仅检索用户的姓名。 Ebean 有这种 [部分对象] 的概念,可以很容易地从中获取你想要的东西 1
假设我有 2 个实体:问题和用户,我想 return 包含问题和用户信息的对象。我怎样才能让 Ebean 加入并将结果绑定到新的 class? (我的意图是将有 1 个 select 连接查询到数据库,而不是获取问题,然后针对每个问题查询将用户信息绑定到它们)。
例如:
@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 ...... "
网络上的大多数示例都与过滤有关。在这种情况下,我不关心 where 条件。
感谢您的回答。
我想你想要的是这个(在这个例子中我假设一个 id 属性 类型为 Long):
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();
这将获取所有 Question 实例并与 UserTable 进行连接,但仅检索用户的姓名。 Ebean 有这种 [部分对象] 的概念,可以很容易地从中获取你想要的东西 1