Spring Hibernate SessionFactory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8121461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:44:56  来源:igfitidea点击:

Spring Hibernate SessionFactory

hibernatespringsessionfactory

提问by DD.

How do you create a SessionFactory using the java config?

如何使用 java 配置创建 SessionFactory?

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

This doesnt work for some reason...it always returns null.

由于某种原因,这不起作用......它总是返回空值。

采纳答案by Chris Beams

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

值得注意的是,Spring 3.1 引入了 LocalSessionFactoryBuilder,它是专门为在 @Bean 方法中使用而设计的。

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

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.

这绕过了处理 FactoryBeans、getObject() 方法等的尴尬需求。FactoryBeans 非常适合在 XML 中使用,但在 @Bean 方法中并不理想。

Note that this new builder is Hibernate 4.1+ only.

请注意,这个新构建器仅适用于 Hibernate 4.1+。

回答by Tomasz Nurkiewicz

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 SessionFactorydirectly somewhere in code, add this helper method:

如果您需要SessionFactory直接在代码中的某处注入,请添加此辅助方法:

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

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

请注意,助手sessionFactory()带注释@Bean-这是非常重要的。

回答by danny.lesnik

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

Tomasz 是对的,但我确实相信使用“new”创建对象实例不符合 Spring 概念:

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:

然后你可以在你的 Spring bean 中使用它:

@Autowired
    SessionFactory sessionFactory;

and then inside of your method:

然后在你的方法里面:

Session session = sessionFactory.getCurrentSession();

回答by phil294

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

此外,如果您注入了EntityManager,则可以通过以下方式获取当前会话

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

回答by Josh

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

设置所有属性后,您应该在会话工厂上调用 afterPropertiesSet()

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();
}