我的休眠实体如下:
@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");
您需要添加别名才能到达具有要查询其属性的对象。