4

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?

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
user820688
  • 719
  • 2
  • 13
  • 27

3 Answers3

1

You should use criteria.createCriteria instead of criteria.createAlias

Jeroen
  • 979
  • 1
  • 7
  • 16
1

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.

RAS
  • 8,100
  • 16
  • 64
  • 86
1

I found the source of problem, it is a stupid error on my part, in my original code I put compny.companyName, I forgot an "a" in company, it works very well.

user820688
  • 719
  • 2
  • 13
  • 27