2010-08-02 45 views
1

我的休眠實體如下:然後Hibernate的標準API多次join

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

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

文件看起來是這樣的:

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

基本上我想寫的查詢是:

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

如何用hibernate標準api來做到這一點?

回答

8

應該這樣做:

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

您需要添加別名,達到具有其上您要查詢的屬性的對象。

+0

是的,我想我可能已經失蹤那只是沒有辦法來測試它。謝謝! – 2010-08-03 01:20:06

0

因此,它看起來像你只是想獲得具有DocumentType id ='xyz'的文檔的EditLocks。我假設文檔和DocumentType有標準的Hibernate映射:

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

休眠應該能夠找出連接你我的想法。

+1

這並不工作:org.hibernate.QueryException: 致無法解析屬性:的document.documentType.id:com.docfinity.document.entity.EditLock – Khandelwal 2010-08-02 16:45:14