views:

104

answers:

2

My hibernate entities are as follows:

@Entity
@Table(name = "EditLocks")
public class EditLock extends AuditableEntity {

    /** The document that is checked out. */
    @OneToOne
    @JoinColumn(name = "documentId", nullable = false)
    private Document document;

Document then looks like this:

public class Document extends AuditableEntity {
    /** Type of the document. */
    @ManyToOne
    @JoinColumn(name = "documentTypeId", nullable = false)
    private DocumentType documentType;

Essentially the query I want to write is:

Select * from EditLocks el, Document docs, DocumentTypes dt where el.documentId = docs.id and docs.documentTypeId = dt.id and dt.id = 'xysz';

How do I do this with the hibernate criteria api?

A: 

So it looks like you are just trying to get the EditLocks who have Documents with a DocumentType id = 'xyz'. I'm assuming that Document and DocumentType have standard Hibernate mappings:

Criteria crit = getSession().createCriteria(EditLock.class);
crit.add(Restrictions.eq("document.documenttype.id", "xyz");

Hibernate should be able to figure out the joins for you I think.

tkeE2036
That doesn't work:Caused by: org.hibernate.QueryException: could not resolve property: document.documentType.id of: com.docfinity.document.entity.EditLock
Khandelwal
+2  A: 

That should do it:

Criteria criteria = getSession().createCriteria(EditLock.class);
criteria.createAlias( "document", "document" );
criteria.createAlias( "document.documentType", "documentType" );
criteria.add(Restrictions.eq("documenttype.id", "xyz");

You need to add aliases to reach the object having the property on which you want to query.

Manuel Darveau
Yeah I thought I might have been missing that just didn't have a way to test it. THanks!
tkeE2036