gpt4 book ai didi

java - Hibernate查询异常: could not resolve property

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:08 25 4
gpt4 key购买 nike

所以我在这三个表之间有这些关系 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%');

由于我使用 hibernate,所以我使用了 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 为何不知道 Project 实体的属性 designation ?!!

最佳答案

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

如果是这样,您可以尝试为 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();

关于java - Hibernate查询异常: could not resolve property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39265397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com