【问题标题】:Hibernate Criteria API multiple joinsHibernate Criteria API 多重连接
【发布时间】:2010-08-02 16:14:32
【问题描述】:

我的休眠实体如下:

@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 执行此操作?

【问题讨论】:

    标签: java hibernate orm criteria hibernate-criteria


    【解决方案1】:

    应该这样做:

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

    您需要添加别名才能访问具有您要查询的属性的对象。

    【讨论】:

    • 是的,我想我可能错过了只是没有办法测试它。谢谢!
    【解决方案2】:

    所以看起来您只是想获取拥有 DocumentType id = 'xyz' 的文档的 EditLock。我假设 Document 和 DocumentType 具有标准的 Hibernate 映射:

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

    我认为 Hibernate 应该能够为你找出连接。

    【讨论】:

    • 这不起作用:原因:org.hibernate.QueryException:无法解析属性:document.documentType.id of:com.docfinity.document.entity.EditLock
    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 2012-08-18
    • 2012-04-09
    • 1970-01-01
    • 2023-03-19
    • 2016-07-12
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多