I am not sure whats the best way to inject Hibernate's session instance to DAO classes using Spring3. I am not using Spring's Hibernate Template support for this so here is the code i have in the DAO class.
public void setSessionFactory(SessionFactory sessionFactory){ this.sessionFactory=sessionFactory; } public SessionFactory getSessionFactory(){ log.info("Returning a refrence to the session instance"); if(sessionFactory==null){ log.error("Not able to find any associated session"); throw new RuntimeException("Not able to find any associated session"); } return sessionFactory; }
Below is the code for injecting session in to this method
<bean id="genericSessionFactory" class="HibernateSessionFactory" factory-method="getSessionfactory" scope="prototype/>
I am not sure if this is the best way to do SessionFactory injection since we don't want to use Spring Template for our project. So any other suggestion for improvement will be much helpfull.
There are two approaches to Spring's Hibernate integration: Inversion of Control with a HibernateTemplate and Callback. Extending HibernateDaoSupport and Applying an AOP Interceptor.
Below spring bean configuration file will work for Spring 4 and Hibernate 4 versions. For hibernate 4, we need to use org. springframework. orm.
The Spring Reference suggests this usage:
public class ProductDaoImpl implements ProductDao { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public Collection loadProductsByCategory(String category) { return this.sessionFactory.getCurrentSession() .createQuery( "from test.Product product where product.category=?") .setParameter(0, category) .list(); } }
That way your classes don't have any dependencies to Spring, you just use plain Hibernate.
A combination of skaffman's post and Sean's plus the use of annotations.
Dao
@Respository("productDao") public class ProductDaoImpl implements ProductDao { @Autowired private SessionFactory sessionFactory; public Collection loadProductsByCategory(String category) { return this.sessionFactory.getCurrentSession() .createQuery( "from test.Product product where product.category=?") .setParameter(0, category) .list(); } }
xml
<beans> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/> <property name="username" value="sa"/> <property name="password" value=""/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="mappingResources"> <list> <value>product.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.HSQLDialect </value> </property> </bean> </beans>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With