1

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 !!!

3 Answers 3

4

Try adding alias :

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

And later call

 criteria.add(Restrictions.ne("docMaster.status",FMSConstants.CLOSED_STATUS));
Sign up to request clarification or add additional context in comments.

1 Comment

this works , Thanx a lott , but I dont understand why , because I am not doing it as you suggested in other cases , but it works fine. Why in this case I had to give an alias.
2

You have to add an alias to docMaster first

4 Comments

but recipientDetail is also an object of another class , UserDetails, and I am reffering to its field the same, why is the hibernate not throwing an exception for that.
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@FurquanAhmed You're right, you'll probably need one there. Note sometimes Hibernate parses stuff "backwards" (it basically builds up a parse stack).
@HarshalPatil, it does provide an answer, though it was light on details. The accepted answer is the same, just more details (not objecting to that one being accepted, it's certainly clearer).
1

I think that it's the comparaison with the enums that are not correct. You are trying to compare an enum with a String. This line seems wrong:

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

Since documentMovement.docMaster.status is defined as a String, maybe try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.