[英]Hibernate Criteria API multiple joins
我的休眠實體如下:
@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';
我該如何使用休眠條件API?
應該這樣做:
Criteria criteria = getSession().createCriteria(EditLock.class);
criteria.createAlias( "document", "document" );
criteria.createAlias( "document.documentType", "documentType" );
criteria.add(Restrictions.eq("documenttype.id", "xyz");
您需要添加別名才能到達具有要查詢其屬性的對象。
因此,看來您只是在嘗試獲取具有DocumentType id ='xyz'的Document的EditLocks。 我假設Document和DocumentType具有標准的Hibernate映射:
Criteria crit = getSession().createCriteria(EditLock.class);
crit.add(Restrictions.eq("document.documenttype.id", "xyz");
我認為,Hibernate應該能夠為您找出聯接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.