无法解析条件 API 中的嵌入实体
Not able to resolve an embedded entity in Criteria API
我正在尝试使用条件 API 对嵌入实体的变量进行限制来执行提取。但是我收到以下错误,
org.hibernate.QueryException: could not resolve property: cpm of: org.sorabh.SystemEnt
下面是我的 SystemEnt 实体,
@Entity
@Table(name="system")
public class SystemEnt implements Serializable{
@Id
@Column(name="pmTpId", nullable = false)
private int id;
@Column(name="pmMeasureId", nullable = false)
private int pmMeasureId;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="pmMeasureId", insertable=false, updatable=false)
private Cpm cpm;
private String mapper;
public int getId() {
return id;
}
public int getPmMeasureId() {
return pmMeasureId;
}
public Cpm getCpm() {
return cpm;
}
public String getMapper() {
return mapper;
}
public void setId(int id) {
this.id=id;
}
public void setPmMeasureId(int pmMeasureId) {
this.pmMeasureId=pmMeasureId;
}
public void setCpm(Cpm cpm) {
this.cpm=cpm;
}
public void setMapper(int mapper) {
this.mapper=mapper;
}
我的 CpmMeas 包含
@Entity
@Table(name="cpm")
public class Cpm implements Serializable{
@Id
@Column(name="pmMeasureId", nullable = false)
private int pmMeasureId;
@Column(nullable=false)
private int pmGranularity
//getters and setters
public void setPmMeasureId(int pmMeasureId) {
this.pmMeasureId=pmMeasureId;
}
public int getPmMeasureId() {
return pmMeasureId;
}
public void setPmGranularity(int pmGranularity) {
this.pmGranularity=pmGranularity;
}
public int getPmGranularity() {
return pmGranularity;
}
我在 serviceImpl 中执行以下操作:
Criteria cr = session.createCriteria(SystemEnt.class);
cr.add(Restrictions.like("mapper", "Test", MatchMode.START));
cr.add(Restrictions.eq("cpm.pmGranularity", 1));
我已经尝试按照 SO 上其他几篇文章中的建议使用别名,但它似乎并没有解决问题。
此外,它在解析映射器时没有问题,即只有嵌套实体和相应的变量没有得到解析。
使用别名,如
Criteria cr = session.createCriteria(SystemEnt.class);
cr.createAlias("cpm","cpm");
cr.add(Restrictions.like("mapper", "Test", MatchMode.START));
cr.add(Restrictions.eq("cpm.pmGranularity", 1));
无论如何,字段映射器应该是一个字符串,在您的示例代码中它被定义为一个 int。
我测试过,毫无疑问它工作正常。
我正在尝试使用条件 API 对嵌入实体的变量进行限制来执行提取。但是我收到以下错误,
org.hibernate.QueryException: could not resolve property: cpm of: org.sorabh.SystemEnt
下面是我的 SystemEnt 实体,
@Entity
@Table(name="system")
public class SystemEnt implements Serializable{
@Id
@Column(name="pmTpId", nullable = false)
private int id;
@Column(name="pmMeasureId", nullable = false)
private int pmMeasureId;
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="pmMeasureId", insertable=false, updatable=false)
private Cpm cpm;
private String mapper;
public int getId() {
return id;
}
public int getPmMeasureId() {
return pmMeasureId;
}
public Cpm getCpm() {
return cpm;
}
public String getMapper() {
return mapper;
}
public void setId(int id) {
this.id=id;
}
public void setPmMeasureId(int pmMeasureId) {
this.pmMeasureId=pmMeasureId;
}
public void setCpm(Cpm cpm) {
this.cpm=cpm;
}
public void setMapper(int mapper) {
this.mapper=mapper;
}
我的 CpmMeas 包含
@Entity
@Table(name="cpm")
public class Cpm implements Serializable{
@Id
@Column(name="pmMeasureId", nullable = false)
private int pmMeasureId;
@Column(nullable=false)
private int pmGranularity
//getters and setters
public void setPmMeasureId(int pmMeasureId) {
this.pmMeasureId=pmMeasureId;
}
public int getPmMeasureId() {
return pmMeasureId;
}
public void setPmGranularity(int pmGranularity) {
this.pmGranularity=pmGranularity;
}
public int getPmGranularity() {
return pmGranularity;
}
我在 serviceImpl 中执行以下操作:
Criteria cr = session.createCriteria(SystemEnt.class);
cr.add(Restrictions.like("mapper", "Test", MatchMode.START));
cr.add(Restrictions.eq("cpm.pmGranularity", 1));
我已经尝试按照 SO 上其他几篇文章中的建议使用别名,但它似乎并没有解决问题。 此外,它在解析映射器时没有问题,即只有嵌套实体和相应的变量没有得到解析。
使用别名,如
Criteria cr = session.createCriteria(SystemEnt.class);
cr.createAlias("cpm","cpm");
cr.add(Restrictions.like("mapper", "Test", MatchMode.START));
cr.add(Restrictions.eq("cpm.pmGranularity", 1));
无论如何,字段映射器应该是一个字符串,在您的示例代码中它被定义为一个 int。
我测试过,毫无疑问它工作正常。