我的项目中有以下课程。
员工类
@XmlRootElement
@Entity
@Table(name = "emp", catalog = "emps")
public class Employee implements java.io.Serializable {
//other entity variables
private Set<Title> titles = new HashSet<Title>(0);
@XmlTransient
@OneToMany(targetEntity = Title.class, fetch = FetchType.EAGER, mappedBy = "employee", cascade=CascadeType.ALL)
public Set<Title> getTitles() {
return this.titles;
}
public void setTitles(Set<Title> titles) {
this.titles = titles;
}
}
标题
@Entity
@Table(name = "titles", catalog = "emps")
public class Title implements java.io.Serializable {
private TitleId id;
private Employee employee;
private Date fromDate;
private Date toDate;
public Title() {
}
public Title(TitleId id, Employee employee) {
this.id = id;
this.employee = employee;
}
public Title(TitleId id, Employee employee, Date toDate) {
this.id = id;
this.employee = employee;
this.toDate = toDate;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "empNo", column = @Column(name = "emp_no", nullable = false)),
@AttributeOverride(name = "title", column = @Column(name = "title", nullable = false, length = 50)),
@AttributeOverride(name = "fromDate", column = @Column(name = "from_date", nullable = false, length = 10)) })
public TitleId getId() {
return this.id;
}
public void setId(TitleId id) {
this.id = id;
}
//rest of the code
}
标题 ID
@Embeddable
public class TitleId implements java.io.Serializable {
private int empNo;
private String title;
private Date fromDate;
public TitleId() {
}
public TitleId(int empNo, String title, Date fromDate) {
this.empNo = empNo;
this.title = title;
this.fromDate = fromDate;
}
@Column(name = "emp_no", nullable = false)
public int getEmpNo() {
return this.empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
@Column(name = "title", nullable = false, length = 50)
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name = "from_date", nullable = false, length = 10)
public Date getFromDate() {
return this.fromDate;
}
public void setFromDate(Date fromDate) {
this.fromDate = fromDate;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof TitleId))
return false;
TitleId castOther = (TitleId) other;
return (this.getEmpNo() == castOther.getEmpNo())
&& ((this.getTitle() == castOther.getTitle()) || (this
.getTitle() != null && castOther.getTitle() != null && this
.getTitle().equals(castOther.getTitle())))
&& ((this.getFromDate() == castOther.getFromDate()) || (this
.getFromDate() != null
&& castOther.getFromDate() != null && this
.getFromDate().equals(castOther.getFromDate())));
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getEmpNo();
result = 37 * result
+ (getTitle() == null ? 0 : this.getTitle().hashCode());
result = 37 * result
+ (getFromDate() == null ? 0 : this.getFromDate().hashCode());
return result;
}
}
我需要创建一个条件来查找用户的名字、姓氏和头衔。名字和姓氏在我的标准中工作正常。以下代码显示了我如何为标题创建标准。“jobTitle”是我传递给它的参数。
Criteria employeeSearchCriteria = getSession().createCriteria(Employee.class);
employeeSearchCriteria.setFirstResult(0);
employeeSearchCriteria.setMaxResults(6);
employeeSearchCriteria.createAlias("titles.TitleId", "titleId");
employeeSearchCriteria.add(Restrictions.eq("titleId.title", jobTitle));
当我运行代码时,它会抛出一个 Query Exception 说org.hibernate.QueryException: could not resolve property: TitleId of: Title
。任何人都可以在这件事上帮助我吗?