Hibernate QueryException: Cannot resolve property
SlimenTN
So I have these relationships between these 3 tables. What I want to do is to search (by or ) and get a list of projects for a specific responsible person like this: code
designation
SELECT * FROM Project p JOIN ProjectHasChief phc ON (p.id = phc.idproject)
JOIN Chief c ON (c.id = phc.id_chief)
WHERE c.id = (myID)
AND (designation LIKE '%user_string%' OR code LIKE '%user_string%');
And since I'm using hibernate I used Criteria
:
Criteria c = session.createCriteria(ProjectHasChief.class);
c.add(Restrictions.eq("chief", chief))
.add(Restrictions.disjunction()
.add(Restrictions.like("project.designation", reg, MatchMode.ANYWHERE))
.add(Restrictions.like("project.code", reg, MatchMode.ANYWHERE)));
List<ProjectHasChief> list = (List<ProjectHasChief>)c.list();
But I get this error:
org.hibernate.QueryException: could not resolve property: project.designation of: smt.agm.entities.ProjectHasChief
Project unit :
@Entity
@Table(name="projet")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition="serial")
private int id;
@Column(name="code")
private String code;
@Column(name="designation")
private String designation;
@OneToMany(cascade = CascadeType.ALL, mappedBy="project", orphanRemoval=true)
@OrderBy(clause="id DESC")
private List<ProjectHasChief> chiefs = new ArrayList<ProjectHasChief>();
}
Chief Entity:
@Entity
@Table(name="chief")
public class Chief {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition="serial")
private int id;
@Column(name = "name")
private String name;
}
ProjectHasChief entity:
@Entity
@Table(name="mapping_chantier_chef")
public class ProjectHasChief {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition="serial")
private int id;
@ManyToOne
private Project project;
@ManyToOne
private Chief chief;
@Column(name="date_deb")
private Date startDate;
@Column(name="date_fin")
private Date endDate;
}
WHE hibernate doesn't know properties designation
of Project
entities? !
Rorschach
Are you absolutely sure that all mappings and joins are working correctly?
If so, you can try declaring an implicit alias for Project like this:
Criteria c = session.createCriteria(ProjectHasChief.class);
c.createAlias("project", "project") //THIS ONE
c.add(Restrictions.eq("chief", chief))
c.add(Restrictions.disjunction()
.add(Restrictions.like("project.designation", reg, MatchMode.ANYWHERE))
.add(Restrictions.like("project.code", reg, MatchMode.ANYWHERE)));
List<ProjectHasChief> list = (List<ProjectHasChief>)c.list();