Hibernate Criteria API 多重连接

发布于 2024-09-13 05:07:12 字数 726 浏览 6 评论 0原文

我的休眠实体如下:

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

My hibernate entities are as follows:

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

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

Document then looks like this:

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

Essentially the query I want to write is:

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

How do I do this with the hibernate criteria api?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清欢 2024-09-20 05:07:12

应该可以做到:

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

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

That should do it:

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

You need to add aliases to reach the object having the property on which you want to query.

末骤雨初歇 2024-09-20 05:07:12

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

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

Hibernate 应该能够为您找出连接。

So it looks like you are just trying to get the EditLocks who have Documents with a DocumentType id = 'xyz'. I'm assuming that Document and DocumentType have standard Hibernate mappings:

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

Hibernate should be able to figure out the joins for you I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文