简体   繁体   English

使用Hibernate创建createCriteria和beginTransaction的顺序有什么不同?

[英]Is there any difference in which order I createCriteria and beginTransaction using Hibernate?

Just wondering is there any difference when I beginTransaction [org.hibernate] before or after creating Criteria/Query etc.? 只是想知道在创建Criteria / Query等之前或之后开始beginTransaction [org.hibernate]有什么区别吗?

example 1: 范例1:

...
Transaction tx= session.beginTransaction();
Criteria c = session.createCriteria(class);
result = c.uniqueResult();
tx.commit();
...

example 2: 范例2:

...
Criteria c = session.createCriteria(class);
Transaction tx= session.beginTransaction();
result = c.uniqueResult();
tx.commit();
...

Thanks. 谢谢。

Creating a transaction before executing crteria is enough. 在执行条件之前创建一个transaction就足够了。

There will be no difference of that line.All you need is a trnasaction before you are executing the criteria. 这条线没有任何区别。执行criteria.之前,您需要做的是一次trnasaction criteria.

There is no difference as transaction matters when you actually working on the database. 实际使用数据库时,事务无关紧要。 Creating criteria will not make any change to the database until you don't execute it. 创建条件不会对数据库进行任何更改,除非您不执行它。 You may even take creation of criteria out of transactions. 您甚至可以从事务中删除标准的创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM