2014-09-21 62 views
0

時,我有以下四種模式:播放2.2.2 Ebean - 錯誤獲取豆描述查詢一對多

兼容性:

@Entity 
public class Compatibility extends Model { 

    @Id 
    public long id; 

    @ManyToOne 
    public Attribute attr1; 

    @ManyToOne 
    public Attribute attr2; 

    public static Finder<Long, Compatibility> find = new Finder<Long, Compatibility>(
      Long.class, Compatibility.class); 


} 

屬性:

@Entity 
public class Attribute extends Model { 

    @Id 
    public long id; 

    public String userId; 

    @ManyToOne 
    public Parameter parameter; 

    public static Finder<Long, Attribute> find = new Finder<Long, Attribute>(
      Long.class, Attribute.class); 

} 

參數:

@Entity 
public class Parameter extends Model { 

    @Id 
    public long id; 

    @ManyToOne 
    public Problem problem; 

    public List<Attribute> attributes = new ArrayList<Attribute>(); 

    public static Finder<Long, Parameter> find = new Finder<Long, Parameter>(
      Long.class, Parameter.class); 

} 

問題:

@Entity 
public class Problem extends Model { 

    @Id 
    public long id; 

    @OneToMany(cascade = CascadeType.ALL) 
    public List<Parameter> parameters = new ArrayList<Parameter>(); 

    public static Finder<Long, Problem> find = new Finder<Long, Problem>(
      Long.class, Problem.class); 
} 

使用Ebean,我試圖篩選,這取決於他們的問題屬於使用該Java代碼的所有兼容性:

List<Compatibility> cs = Ebean.find(Compatibility.class) 
       .fetch("attribute").fetch("parameter").fetch("problem").where() 
       .eq("problem.id", problemId).findList(); 

然而,我造成此錯誤:

[RuntimeException: Error getting BeanDescriptor for path problem from models.Compatibility] 

我的猜測是跨幾個表的映射不起作用。有沒有任何我可以做到這一點?手動查詢也許?

回答

0

我不知道這是否是問題,但問題與兼容性之間沒有提及。您應該嘗試向類Compatibility添加List屬性。 Ebean無法知道它是否必須在Compatibility.attr1和Attribute或Compatibility.attr2和Attribute之間進行鏈接。

我不使用個人提取。我發現這個功能很難理解和調試(你面臨的情況並不罕見)。我寧願用手寫sql中的複雜查詢並使用此函數檢索列表:

/** 
* Function which process a sql query returning a list of object. 
* @param req  the sql query 
* @param classObj class object of the class to return 
* @return the list matching the req 
*/ 
public static <T> List<T> getListFromSql(String req, Class<T> classObj) 
{ 
    List<T>   lst; 

    RawSql rawSql = RawSqlBuilder 
      .parse(req) 
      .create(); 
    Query<T> query = Ebean.find(classObj); 
    query.setRawSql(rawSql); 
    lst = query.findList(); 
    return lst; 
}