Search code examples
javahibernatecriteriamany-to-one

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


I have a class DocMovement like this :

    @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
    }

The DocMaster class is something like this :

@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
}

When I am trying to run following code :

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

then I get the following exception :

 [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

there are other cases where I try to make query using criteria as shown above, the query runs fine, but I am unable to understand what am I doing wrong in this case. I have tried using eager fetch too, earlier I was not using an alias, so I tried using an alias too.

Please help me solve the issue !!!


Solution

  • Try adding alias :

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

    And later call

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