在Spring Hibernate XML配置中,我有
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
// ...
<property name="annotatedClasses">
<list>
<value>com.abc.xyz.Foo</value>
// ...
</list>
</property>
<property name="hibernateProperties">
<props>
// ....
</props>
</property>
//..
</bean>
我知道Spring Java配置的等价物如下: 。LocalSessionFactoryBuilder(数据源())addAnnotatedClasses(让Foo.class).buildSessionFactory();
我的问题是如果不使用LocalSessionFactoryBuilder类,而是使用HibernateJpaVendorAdapter和LocalContainerEntityManagerFactoryBean类,如何做同样的事情?
答案 0 :(得分:1)
您可以通过以下方式执行此操作:
@Bean
public AbstractEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan("com.abc.xyz.Foo");
entityManagerFactory.setPersistenceProvider(new HibernatePersistence());
entityManagerFactory.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", "validate");
return entityManagerFactory;
}