English 中文(简体)
休眠会议
原标题:Hibernate Sessions

我在学习休眠时遇到一个问题。 我写了 UnitedTest, 并试图在 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", "[email protected]", "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", "[email protected]", "HiberSurname", "HiberLastname", "HiberAddress", "123432"));
    sessionFactory.getCurrentSession().getTransaction().commit();
}
}

这让我觉得很例外 交易没有顺利启动 但它增加了用户

这里有一个例外

org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:679) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:845) at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:822) at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:512) at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:290) at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:183) at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:406) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:90) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: org.hibernate.TransactionException: Transaction not successfully started at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183) at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:676) ... 25 more

问题回答

我看到你代码里有两个问题

- 您的交易尚未启动 。

回答:在数据库上做任何事情之前,您必须先使用获取会议(. beginTransaction ()) 的方法。

- 交易失败时没有回滚方法

答复:使用试捕渔获物块,见下文的例子。

我的代码示例:

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();
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签