Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the mention about HibernateTemplate not being the correct way of doing things any more, and the suggestion of the @Transactional annotation, I've come up with the following which seems to work. I make no claims that this is the best or only way but it works and I want to keep a record of this config for future readers.</p> <p>Note that the "old way" of using HibernateTemplate is the way described in the book <strong>"Spring in Action 2nd Edition"</strong> but this new way mostly comes from the <strong>3rd Edition</strong>.</p> <p>Spring config</p> <pre><code>&lt;bean id="FooDao" class="com.foo.dao.FooDao" &gt; &lt;constructor-arg name="sessionFactory" ref="sessionFactory"/&gt; &lt;/bean&gt; &lt;tx:annotation-driven /&gt; &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory"&gt;&lt;ref local="sessionFactory"/&gt;&lt;/property&gt; &lt;/bean&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="configLocation"&gt; &lt;value&gt;WEB-INF/hibernate.cfg.xml&lt;/value&gt; &lt;/property&gt; &lt;property name="packagesToScan"&gt; &lt;list&gt; &lt;value&gt;com.secretry.domain&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="mappingResources"&gt; &lt;list&gt; &lt;value&gt;com/secretry/domain/Mission.hbm.xml&lt;/value&gt; &lt;value&gt;com/secretry/domain/MissionType.hbm.xml&lt;/value&gt; &lt;value&gt;com/secretry/domain/Spy.hbm.xml&lt;/value&gt; &lt;value&gt;com/secretry/domain/CompletedMission.hbm.xml&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>Java code for DAO I use to get objects from Hibernate.</p> <pre><code>@Repository @Transactional public class FooDao implements IFooDao { private SessionFactory sessionFactory; @Autowired public FooDao(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public FooDao() { } private Session currentSession() { return sessionFactory.getCurrentSession(); } @Override @Cacheable(cacheName="secretryCache") public Foo getFoo(int id) { return (Foo)currentSession().load(Foo.class, id); } } </code></pre> <p>Make sure to remove the following from your hibernate config file if you have it</p> <pre><code> &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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