【问题标题】:Hibernate criteria for OneToMany/ManyToOne relationshipOneToMany/ManyToOne 关系的休眠条件
【发布时间】:2012-10-05 11:18:45
【问题描述】:

我在两个对象之间存在 OneToMany/ManyToOne 关系,例如:

public class Company {
    private Long compid;
    private String companyName;

    @OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Employee> employees;
}

public class Employee {
    private Long empid;
    private String fstName;
    private String lstName;
    private String sal;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "compid", nullable = true)
    private Company company;
}

我想获取拥有company.companyName like xxxx% 的员工。我试过类似的东西:

Criteria criteria = session.createCriteria(Employee.class);
criteria.createAlias("company", "company");
criteria.add(Restrictions.like("company.companyName ", "xxxx%"));
return criteria.list();

但我得到了错误:

could not resolve property: company.companyName of: Employee

这段代码哪里出了问题?

【问题讨论】:

    标签: java many-to-one hibernate-criteria hibernate-onetomany


    【解决方案1】:

    我找到了问题的根源,这是我的一个愚蠢的错误,在我的原始代码中我输入了 compny.companyName,我忘记了公司中的“a”,它工作得很好。

    【讨论】:

      【解决方案2】:

      您在第 3 行标准中添加了一个空格,例如 "company.companyName "。是错字吗?如果不是,那就是问题的原因。

      【讨论】:

        【解决方案3】:

        您应该使用criteria.createCriteria 而不是criteria.createAlias

        【讨论】:

          猜你喜欢
          • 2011-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-29
          • 2020-02-18
          相关资源
          最近更新 更多