Spring Hibernate SessionFactory

≡放荡痞女 提交于 2019-11-30 22:19:58

Worth noting here that Spring 3.1 introduces LocalSessionFactoryBuilder, which is expressly designed for use within @Bean methods.

http://static.springsource.org/spring/docs/3.1.0.RC1/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.html

This gets around the awkward need to deal with FactoryBeans, getObject() methods, etc. FactoryBeans are excellent for use in XML, but non-ideal in @Bean methods.

Note that this new builder is Hibernate 4.1+ only.

Return factory instead:

@Bean
public AbstractSessionFactoryBean sessionFactoryBean(){
    AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    return sessionFactoryBean;
}

If you need to inject SessionFactory directly somewhere in code, add this helper method:

public SessionFactory sessionFactory() {
    return sessionFactoryBean().getObject();
}

Note that the helper sessionFactory() is not annotated with @Bean - this is really important.

Tomasz is right, but I do believe that creating object instance using "new" does not feet with Spring concept:

I think you need to do it this way:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <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> 
    <prop key="hibernate.show_sql">true</prop> 
  </props>
     </property>
        <property name="annotatedClasses">
      <list>
        <value>com.vanilla.objects.Student</value>
        <value>com.vanilla.objects.Address</value>

         </list>
    </property>
  </bean>

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

and then you can use it inside your Spring bean:

@Autowired
    SessionFactory sessionFactory;

and then inside of your method:

Session session = sessionFactory.getCurrentSession();

Since the above answers are outdated, here's a more modern approach:

@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){
    return hemf.getSessionFactory();
}

Also, if you've injected an EntityManager, you can get the current session via

Session session = entityManager.unwrap(Session.class);

You should call afterPropertiesSet() on the session factory after setting all the properties

So in your example it would look like:

@Bean
public SessionFactory sessionFactory(){
    AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    sessionFactoryBean.afterPropertiesSet();
    return sessionFactoryBean.getObject();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!