关于 Hibernate API 中 Criteria.createCriteria 的问题
因此,我仍在尝试熟悉 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
c = c.createCriteria("nnContractTbl");
上的赋值是多余的,createCriteria
和几乎所有Criteria
方法修改调用它们的实例,并为方法返回实例本身查宁。因此,您可以像这样链接调用:关于该序列的结果,
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 allCriteria
methods modify the instance they're invoked on, and return the instance itself for method chanining. So you can chain calls like this: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 attributeassociation
. It will also "root" (as they state in the Javadocs) the Criteria at the association entity, so that in further calls tocreateCriteria(association)
,association
refers to an association attribute declared on the last "rooted" entity.It's a shorthand for
Criteria.createCriteria(association, joinType)
withjoinType
CriteriaSpecification.INNER_JOIN
.上面指定的那些 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.