2015-10-16 58 views
1

我們假設我有2個實體:問題和用戶,並且我想返回有問題和用戶信息的對象。我怎樣才能讓Ebean加入並將結果綁定到新類? (我的意圖是將有1選擇連接查詢分貝,而不是提取問題,然後爲每個問題查詢綁定用戶信息給他們)。JPA/Ebean select join

例如:

@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 ...... " 

幅材處理過濾上的大多數例子。在這種情況下,我不在乎情況。

感謝您的回答。

回答

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(); 

這將獲取所有問題的實例,並與用戶表聯接,但只檢索用戶的名字。 Ebean擁有[部分對象]的概念,可以輕鬆地從中獲取所需內容1