English 中文(简体)
如何设置自动承诺在与春季MVC的冬眠中假冒
原标题:How to set autocommit to false in hibernate with Spring MVC

目前我正在学习“春+休眠”的整合,到目前为止,我正在让它发挥作用。但我遇到了我不想在过程结束时进行交易的情形,因为我只想看到生成的Sql Statement用于测试和调试目的。

我已经把假的添加到我的冬眠属性, 但后来它仍然不起作用。

如果使用 Spring 处理冬眠交易, 是否有可能实现? 因为使用传统的冬眠交易时, 有可能不拨会议电话。 承诺( ) 方法, 所有更新和插入都不会被保存 ;

目前我的服务层有这个代码:

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) public class EmployeeServiceImpl{

@Autowired
private EmployeeDao employeeDao

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void saveEmployee(Employee employee){

    // count the number of employee in the database
    int n  = employeeDao.getEmployeeCount();
    // lets say it should print 10 here
    System.out.println("number of employee before inserting : " + n);

    // insert to employee
    employeeDao.saveEmployee(employee);

    // then count the employee after inserting
    n  =  employeeDao.getEmployeeCount();

    // then it should print 11 here after inserting new employee
    System.out.println("number of employee after inserting : " + n);

/* Then Dont commit insert!!!
*/

时 时

时 时

Dao 层代码 :

public EmployeeDaoImpl implements EmployeeDao{

@Autowired
public void saveEmployee(Employee employee){

    sessionFactory.getCurrentSession.save(employee);

时 时

public int getEmployeeCount(){

    String count = sessionFactory.getCurrentSesion.createQuery("select count(emp.employeeId) from Employee emp")
    .list().get(0).toString;

return Integer.valueOf(count);
时 时

时 时

但这里发生的事情是,它确实进行交易!!但我不想仅仅为了测试和调试目的进行交易。

I ve also tried @Transactional(propagation = Propagation.SUPPORTS, readOnly = false) instead of @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)

但这里发生的情况是,它并不进行交易,但雇员的计数也不加薪。

因此,我期待在这里发生的是,在插入到雇员表之前先计算雇员人数。然后插入雇员表,再次计算雇员人数,这样就会增加1。 但是,在程序结束时,我不想承诺插入!

你们有想法的人吗?

I would be very thankful for any help! Thanks!

最佳回答

用于测试, 请使用 < a href=" http://static. springsource. org/ spring/docs/3. 0. x/ spring- framework- reference/ html/ aidate. html# Incivil- suggest" rel= “ nofollow” > Spring 测试框架 < / a> 。 它处理自动交易回滚。 如果您重新寻找其它的东西, 您将不得不更具体一些。 您的问题虽然详细, 但对于您实际想要做什么却相当模糊。 您只需使用“ 测试” 和“ 调试” 等词, 这些词对不同的人意味着很多不同的东西 。

问题回答

暂无回答




相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签