OneToMany / ManyToOne关系的Hibernate标准

时间:2012-10-15 13:14:12

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

我在两个对象之间有一个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

此代码中的问题在哪里?

3 个答案:

答案 0 :(得分:1)

您已在第3行标准中添加了"company.companyName "之类的空格。这是错字吗?如果不是那么它就是问题的原因。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

您应该使用criteria.createCriteria代替criteria.createAlias