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.
The Spring Reference suggests this usage:
That way your classes don’t have any dependencies to Spring, you just use plain Hibernate.