Hibernate - org.hibernate.QueryException:无法解析属性:

时间:2015-06-14 15:14:52

标签: java hibernate criteria many-to-one

我有一个像这样的DocMovement类:

    @Entity
    @Table(name = "DOC_MVMNT")
    public class DocMovement {        
        @Id
        @GeneratedValue
        @Column(name = "MVMNT_ID")
        private int mvmnt_id;

        @ManyToOne
        @JoinColumn(name = "BARCODE")
        public DocMaster docMaster;
    // other fields and getters setters
    }

DocMaster类是这样的:

@Entity 
@Table(name="DOC_MASTER")
public class DocMaster {    
    @Id
    @NotNull
    @Column(name = "BARCODE")
    private String barcode;

    @Column(name = "DOC_NO")
    private String docNo ;

    @Column(name="DOC_TYPE")
    private String docType;

    @Column(name="STATUS")
    private String status;
// other fields and getters setters
}

当我尝试运行以下代码时:

Criteria criteria = session.createCriteria(DocMovement.class,"documentMovement");
        criteria.add(Restrictions.eq("documentMovement.recipientDetail.empId", empId));
        criteria.add(Restrictions.eq("documentMovement.isCurrent", true));
        criteria.add(Restrictions.ne("documentMovement.docMaster.status",FMSConstants.CLOSED_STATUS));
        criteria.add(Restrictions.ne("documentMovement.docMaster.status",FMSConstants.DISPOSED_STATUS));
        List<DocMovement> documentsHeld = (List<DocMovement>) criteria.list();

然后我得到以下异常:

 [org.hibernate.QueryException: could not resolve property:
 docMaster.status of: com.fms.persistence.DocMovement] with root cause
 org.hibernate.QueryException: could not resolve property:
 docMaster.status of: com.fms.persistence.DocMovement

在其他情况下,我尝试使用上面显示的条件进行查询,查询运行正常,但我无法理解在这种情况下我做错了什么。我也尝试过使用eager fetch,之前我没有使用别名,所以我也尝试使用别名。

请帮我解决问题!!!

3 个答案:

答案 0 :(得分:4)

尝试添加别名:

criteria.createAlias("documentMovement.docMaster", "docMaster")

稍后再打电话

 criteria.add(Restrictions.ne("docMaster.status",FMSConstants.CLOSED_STATUS));

答案 1 :(得分:2)

您必须先向docMaster添加别名

答案 2 :(得分:1)

我认为这是与不正确的枚举的比较。您正在尝试将枚举与String进行比较。这条线似乎错了:

criteria.add(Restrictions.ne("documentMovement.docMaster.status",FMSConstants.CLOSED_STATUS));

由于documentMovement.docMaster.status被定义为String,因此可以尝试:

criteria.add(Restrictions.ne("documentMovement.docMaster.status",FMSConstants.CLOSED_STATUS.toString()));
criteria.add(Restrictions.ne("documentMovement.docMaster.status",FMSConstants.DISPOSED_STATUS.toString()));