Hibernate QueryException:无法解析属性

时间:2016-09-01 07:34:00

标签: java hibernate criteria

所以这三张桌子之间有这些关系 check this pic
我想要做的是搜索(通过codedesignation)并获取特定负责人的项目列表,如下所示:

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%');

因为我正在使用休眠,所以我使用了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();

但是我收到了这个错误:

org.hibernate.QueryException: could not resolve property: project.designation of: smt.agm.entities.ProjectHasChief

项目实体

@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>();
}

主要实体:

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

什么时候hibernate不知道designation实体的属性Project?!!

1 个答案:

答案 0 :(得分:0)

您是否绝对确定所有映射和连接确实有效?

如果是这样,你可以尝试像这样声明Project的隐式别名:

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();