关于 Hibernate API 中 Criteria.createCriteria 的问题

发布于 2024-12-05 16:45:18 字数 797 浏览 0 评论 0原文

因此,我仍在尝试熟悉 Hibernate Criteria API,并且我有一段 Java 代码,我想寻求澄清。

Criteria c = super.getSession().createCriteria(PpNnCtDetail.class);
c.add(Restrictions.between("commencementDate", fromDate, toDate);

这部分我明白发生了什么 - 用 SQL 来说,它会是这样的 - 如果我错了,请纠正我,

SELECT * FROM PpNnCtDetail WHERE commencementDate >= fromDate AND commencementDate <= toDate;

问题出在上面两行后面的代码上。

c = c.createCriteria("nnContractTbl");
c.createCriteria("progCategory").add(Restrictions.in("progCategoryId", allProgCat));
c.createCriteria("acadOrgTbl");
c.createCriteria("serviceType");
c.createCriteria("campusTbl");
return c.list();

第一行试图完成什么?回到 c 的分配是多余的吗?

事实上,c.createCriteria 试图实现什么目标?等效的 SQL 查询会是什么样子?更重要的是,c.list()会返回什么?

So I'm still trying to get myself acquainted with the Hibernate Criteria API, and I have this piece of Java code which I would like to seek clarification over.

Criteria c = super.getSession().createCriteria(PpNnCtDetail.class);
c.add(Restrictions.between("commencementDate", fromDate, toDate);

This part I understand what's happening - putting it in terms of SQL it would be something like - correct me if I'm wrong,

SELECT * FROM PpNnCtDetail WHERE commencementDate >= fromDate AND commencementDate <= toDate;

The problem comes with the code following the two lines above.

c = c.createCriteria("nnContractTbl");
c.createCriteria("progCategory").add(Restrictions.in("progCategoryId", allProgCat));
c.createCriteria("acadOrgTbl");
c.createCriteria("serviceType");
c.createCriteria("campusTbl");
return c.list();

What is the first line trying to accomplish? Is that assignment back to c redundant?

In fact, what are the lines c.createCriteria trying to achieve? What would an equivalent SQL query look like? More importantly, what would c.list() return?

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

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

发布评论

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

评论(2

转瞬即逝 2024-12-12 16:45:19

c = c.createCriteria("nnContractTbl"); 上的赋值是多余的,createCriteria 和几乎所有Criteria 方法修改调用它们的实例,并为方法返回实例本身查宁。因此,您可以像这样链接调用:

return super.getSession().createCriteria(PpNnCtDetail.class)
    .add(Restrictions.between("commencementDate", fromDate, toDate)
    .createCriteria("nnContractTbl")
    .createCriteria("progCategory").add(Restrictions.in("progCategoryId", allProgCat))
    .createCriteria("acadOrgTbl")
    .createCriteria("serviceType")
    .createCriteria("campusTbl")
    .list();

关于该序列的结果, Criteria.createCriteria(association) 将在条件和表中已有的数据之间产生内部联接在由属性 association 建模的关联中设计。它还将“根”(如 Java 文档中所述)关联实体的 Criteria,以便在进一步调用 createCriteria(association) 时,association 引用一个在最后一个“有根”实体上声明的关联属性。

它是 Criteria.createCriteria(association, joinType)joinType CriteriaSpecification.INNER_JOIN 的简写。

The assignament on c = c.createCriteria("nnContractTbl"); is redundant, createCriteria and almost all Criteria methods modify the instance they're invoked on, and return the instance itself for method chanining. So you can chain calls like this:

return super.getSession().createCriteria(PpNnCtDetail.class)
    .add(Restrictions.between("commencementDate", fromDate, toDate)
    .createCriteria("nnContractTbl")
    .createCriteria("progCategory").add(Restrictions.in("progCategoryId", allProgCat))
    .createCriteria("acadOrgTbl")
    .createCriteria("serviceType")
    .createCriteria("campusTbl")
    .list();

And about the result of that sequence, Criteria.createCriteria(association) will result in an inner join between the data already in the criteria and the table designed in the association modelled by the attribute association. It will also "root" (as they state in the Javadocs) the Criteria at the association entity, so that in further calls to createCriteria(association), association refers to an association attribute declared on the last "rooted" entity.

It's a shorthand for Criteria.createCriteria(association, joinType) with joinType CriteriaSpecification.INNER_JOIN.

原谅过去的我 2024-12-12 16:45:19

上面指定的那些 createCriteria 基本上相当于传递的实体的 INNER JOIN。

找到问题答案(同时学习休眠)的最佳方法是转SQL 日志记录 在你的 hibernate 配置文件中,并检查生成的 SQL。

Those createCriteria as specified above are basically equivalent to an INNER JOIN to the entity passed.

The best way to find answers to your questions (and also learn hibernate in the meantime) is to turn SQL logging on in your hibernate configuration file, and inspect the generated SQL.

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