2015-06-14 81 views
1

我有一个类DocMovement这样的:休眠 - org.hibernate.QueryException:无法解析属性:

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

请帮我解决问题!!!

回答

4

尝试增加别名:

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

,过一会儿给

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

这工作,Thanx洛特,但我不明白为什么,因为我没有按照你在其他情况下建议,但它工作正常。为什么在这种情况下,我不得不给别名。 –

2

您必须添加一个别名docMaster第一

+0

bu t recipientDetail也是另一个类UserDetails的一个对象,并且我在回忆它的字段是相同的,为什么hibernate没有为此抛出异常。 –

+0

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 –

+0

@FurquanAhmed你说得对,你可能需要一个。注意有时候Hibernate会“向后”分析东西(它基本上构建了一个解析堆栈)。 – Taylor

1

我认为这是与不正确的枚举的comparaison。你正试图比较一个字符串的枚举。此行似乎是错误的:

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