简体   繁体   中英

Hibernate criteria for OneToMany/ManyToOne relationship

I have a OneToMany/ManyToOne relationship between two objects like:

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;
}

I want to get the employees who have company.companyName like xxxx% . I tried something like:

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

But i get the error:

could not resolve property: company.companyName of: Employee

Where is the problem in this code?

You have added a space like "company.companyName " in 3rd line of criteria. Is it typo? If not then it is the cause of the problem.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM