我有以下四种模式:
兼容性:
@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 :(得分:0)
我不知道这是不是问题,但问题和兼容性之间没有任何参考。您应该尝试将List属性添加到类兼容性中。 Ebean无法知道它是否必须在Compatibility.attr1和Attribute或Compatibility.attr2和Attribute之间建立链接。
我不会亲自使用fetch。我发现这个功能很难理解和调试(你面临的情况并不罕见)。我宁愿手工编写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;
}