user1782784
user1782784

Reputation: 67

Spring 3 and Hibernate 4 Dao

I found HibernateTemplate is removed from Hibernate 4 and how should I configure Dao application context xml file?

Upvotes: 1

Views: 1905

Answers (1)

Paulius Matulionis
Paulius Matulionis

Reputation: 23415

I am using sessionFactory.getCurrentSession() in my DAO classes. And the Spring configuration is like this:

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
        <list>
            <value>my.package.entity.Account</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

So now you should use for e.g.:

@Autowired
private SessionFactory sessionFactory;

public void save(YourEntity entity) {
    sessionFactory.getCurrentSession().save(entity);
}

Change your HibernateTemplate to SessionFactory.

Upvotes: 3

Related Questions