0

我的项目中有以下课程。

员工类

@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。任何人都可以在这件事上帮助我吗?

4

1 回答 1

0

我在这里猜测,但尝试在类Title中将 getter 方法名称从getId()更改为getTitleId()

否则,我只能说我看到的所有示例都@EmbeddedId显示了如何在字段本身上执行此操作,而不是像您那样在 getter 上执行此操作。所以你也可以尝试对字段进行注释(尽管 Hibernate 建议不要混合方法和字段映射注释)。

于 2012-12-30T21:33:10.773 回答