为嵌入式表创建休眠条件时出错

时间:2012-12-30 21:03:03

标签: java hibernate hibernate-criteria

我的项目中有以下课程。

员工类

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

当我运行代码时,它会抛出一个查询异常,说org.hibernate.QueryException: could not resolve property: TitleId of: Title。在这件事上有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

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

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