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

Hibernate - org.hibernate.QueryException: could not resolve property:

我有一个这样的 class 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 class 是这样的:

@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,之前我没有使用别名,所以我也尝试使用别名。

请帮我解决问题!!!

您必须先为docMaster添加一个别名

我认为是与枚举的比较不正确。您正在尝试将枚举与字符串进行比较。这行似乎有误:

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

由于 documentMovement.docMaster.status 被定义为字符串,也许可以尝试:

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

尝试添加别名:

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

稍后调用

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