【问题标题】:JPA/Ebean select joinJPA/Ebean 选择加入
【发布时间】:2015-10-16 12:00:48
【问题描述】:

假设我有 2 个实体:问题和用户,我想返回包含问题和用户信息的对象。我如何要求 Ebean 加入并绑定结果到新类? (我的意图是有 1 个选择连接查询到 db,而不是获取问题,然后为每个问题查询绑定用户信息)。

例如:

@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 条件。

感谢您的回答。

【问题讨论】:

    标签: playframework ebean


    【解决方案1】:

    我想你想要的是这个(在这个例子中我假设一个 Long 类型的 id 属性):

    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 有这个 [partial objects] 的概念,可以很容易地从他们那里获取你想要的东西1

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 2017-10-04
      • 2014-11-08
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多