Hibernate Criteria API多个连接

Kha*_*wal 1 java orm hibernate criteria hibernate-criteria

我的hibernate实体如下:

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

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

文档然后看起来像这样:

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

基本上我想写的查询是:

Select * from EditLocks el, Document docs, DocumentTypes dt where el.documentId = docs.id and docs.documentTypeId = dt.id and dt.id = 'xysz';
Run Code Online (Sandbox Code Playgroud)

如何使用hibernate条件api执行此操作?

Man*_*eau 8

应该这样做:

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

您需要添加别名以到达具有您要查询的属性的对象.