Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Inject Hibernate Session by Spring 3

Tags:

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.

like image 269
Umesh Awasthi Avatar asked Jan 15 '11 11:01

Umesh Awasthi


People also ask

What are the ways to access Hibernate by using spring?

There are two approaches to Spring's Hibernate integration: Inversion of Control with a HibernateTemplate and Callback. Extending HibernateDaoSupport and Applying an AOP Interceptor.

Which spring bean is required to configure Hibernate with spring?

Below spring bean configuration file will work for Spring 4 and Hibernate 4 versions. For hibernate 4, we need to use org. springframework. orm.


2 Answers

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.

like image 153
Sean Patrick Floyd Avatar answered Oct 06 '22 22:10

Sean Patrick Floyd


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> 
like image 41
zawhtut Avatar answered Oct 06 '22 21:10

zawhtut