Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to integrate spring with hibernate session and transaction management?
    primarykey
    data
    text
    <p>I am a beginner in both hibernate and spring. I have understood about the hibernate transaction demarcation (at least I think so). But after coding a few method like this:</p> <pre><code>sessionFactory.getCurrentSession().beginTransaction(); //do something here sessionFactory.getCurrentSession().endTransaction(); </code></pre> <p>I started to want to avoid it and want to have it automatically done outside my method such that I only write the "//do something here" part. I have read about the TransactionProxyFactoryBean and think that the xml configuration is very long and have to be repeated for EVERY classes I want to make transactional so if possible I want to avoid using it.</p> <p>I tried to use the @Transactional but it doesn't work at all. I have these lines in my applicationContext.xml</p> <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="sessionFactory" ref="sessionFactory" /&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; </code></pre> <p>and I have already marked my service classes with @Transactional but I always get the "xxx is not valid without active transaction". Here is an example code that give me an error (run in unit test btw):</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) public class UserServiceTest { @Resource private UserService userService; @Test public void testAddUser() { User us = new User(); us.setName("any name"); userService.addUser(us); } } </code></pre> <p>In this case, the exact error message is: "org.hibernate.HibernateException: save is not valid without active transaction".</p> <p><em>UPDATE</em>: I tried calling the userService.addUser() method from outside unit tests (i.e. from actual web application) and I got the same error as well.</p> <p>This is my hibernate.cfg.xml:</p> <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; &lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;!-- JDBC connection pool (use the built-in) --&gt; &lt;property name="connection.pool_size"&gt;1&lt;/property&gt; &lt;!-- SQL dialect --&gt; &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt; &lt;!-- Enable Hibernate's automatic session context management --&gt; &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt; &lt;!-- Disable the second-level cache --&gt; &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt; &lt;!-- Echo all executed SQL to stdout --&gt; &lt;property name="show_sql"&gt;true&lt;/property&gt; &lt;!-- Drop and re-create the database schema on startup --&gt; &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt; &lt;!-- all my mapping resources here --&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>The userService class is marked with @Transactional. I am using hibernate 3.3.2 and spring 2.5.6.</p> <p>Can I have some advice on how to fix this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload