休眠会话

时间:2012-05-28 12:57:15

标签: java hibernate sessionfactory

我在学习Hibernate时遇到了问题。我写了UnitTest,在那里我尝试将对象添加到DB中。所有选择都正常,但插入不正常。

public class HibernateTutorial extends BaseTest {

@Autowired
SessionFactory sessionFactory;

@Test
@Transactional
public void hibernateTutorial() {

    @SuppressWarnings("unchecked")
    List<User> users = sessionFactory.getCurrentSession().createQuery("from User").list();


    sessionFactory.getCurrentSession().saveOrUpdate(new User("HiberTest", "HiberPass", "Hiber@Mail.ru", "HiberSurname",
    "HiberLastname", "HiberAddress", "123432"));

}

我也尝试这样的事情

public class HibernateTutorial extends BaseTest {

@Autowired
SessionFactory sessionFactory;

@Test
@Transactional
public void hibernateTutorial() {

    @SuppressWarnings("unchecked")
    List<User> users = sessionFactory.getCurrentSession().createQuery("from User").list();


    sessionFactory.getCurrentSession().saveOrUpdate(
            new User("HiberTest", "HiberPass", "Hiber@Mail.ru", "HiberSurname", "HiberLastname", "HiberAddress", "123432"));
    sessionFactory.getCurrentSession().getTransaction().commit();
}
}

它抛出异常,即交易未成功启动,但它添加了用户

这是一个例外

  

org.springframework.transaction.TransactionSystemException:无法   回滚Hibernate事务;嵌套异常是   org.hibernate.TransactionException:事务未成功   开始于   org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:679)     在   org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:845)     在   org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:822)     在   org.springframework.test.context.transaction.TransactionalTestExecutionListener $ TransactionContext.endTransaction(TransactionalTestExecutionListener.java:512)     在   org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:290)     在   org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:183)     在   org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406)     在   org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:90)     在   org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)     在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:193)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:52)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:42)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:184)at at   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:236)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)   引起:org.hibernate.TransactionException:不是事务   成功开始了   org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183)     在   org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:676)     ......还有25个

1 个答案:

答案 0 :(得分:0)

我可以在你的代码中看到2个问题。

1 - 您的交易尚未开始。

答案:在对数据库执行任何操作之前,必须使用getSession()。beginTransaction()方法。

2 - 如果您的交易失败,则没有回滚方法

答案:使用try-catch块,请参阅下面的示例。

我的代码示例:

public void salvarDB(Object object)
{
    Session session = HibernateUtil.getSession();
    HibernateUtil.beginTransaction();
    try
    {
        session.save(object);
        HibernateUtil.commit();
        avisos.salvoComSucesso(object);
    }
    catch(ConstraintViolationException e)
    {
        avisos.registroJaInserido(object);
        HibernateUtil.rollback();
    }
    catch (Exception ex) {
        avisos.falhaAoSalvar(object);
        HibernateUtil.rollback();
    }
    finally {
        HibernateUtil.closeSession();
    }
}