【问题标题】:Play 2.2.2 with Ebean - Error getting Bean Descriptor when Querying OneToMany使用 Ebean 玩 2.2.2 - 查询 OneToMany 时获取 Bean 描述符时出错
【发布时间】:2014-09-21 14:20:32
【问题描述】:

我有以下四种型号:

兼容性:

@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]

我的猜测是跨多个表的映射不起作用。我有任何方法可以完成这项工作吗?也许是手动查询?

【问题讨论】:

    标签: java postgresql playframework ebean


    【解决方案1】:

    我不知道这是否是问题,但问题和兼容性之间没有参考。您应该尝试将 List 属性添加到类 Compatibility。 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-14
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多