问题
How do you create a SessionFactory using the java config?
@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.
回答1:
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.
回答2:
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.
回答3:
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();
回答4:
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);
回答5:
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();
}
来源:https://stackoverflow.com/questions/8121461/spring-hibernate-sessionfactory