我已经坚持了很长一段时间了。我想将遗留应用程序中的Hibernate配置升级到JPA配置,仍然使用Hibernate作为底层实现。版本包括:Hibernate 4.3.11和Spring 4.2.4。
配置采用XML格式,为清晰起见,拆分为较小的配置文件:
datasource.xml
<bean id="hibernateDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/my-db"/>
<property name="resourceRef" value="true"/>
</bean>
实体manager.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="hibernateDataSource"/>
<property name="packagesToScan" ref="hibernatePackagesToScan"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="connection.isolation">2</prop>
<prop key="export">false</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_minimal_puts">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.DefaultComponentSafeNamingStrategy</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.hbm2ddl.auto"/>
<prop key="hibernate.mapping.precedence">class, hbm</prop>
<prop key="hibernate.max_fetch_depth">0</prop>
<prop key="hibernate.multi_tenant_connection_provider">com.myapp.MyMultiTenantConnectionProvider</prop>
<prop key="hibernate.multiTenancy">SCHEMA</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.tenant_identifier_resolver">com.myapp.MyTenantProvider</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
</props>
</property>
</bean>
<!-- For use in beans that depend on session factory -->
<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean"/>
<bean id="hibernatePackagesToScan" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>com.myapp.model</value>
</list>
</constructor-arg>
</bean>
</beans>
交易manager.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
</beans>
DAO扩展了一个如下所示的AbstractDao:
public abstract class AbstractDao
{
@Autowired
private SessionFactory sessionFactory;
protected Session getSession()
{
try
{
return sessionFactory.getCurrentSession();
}
catch (org.hibernate.HibernateException e)
{
return sessionFactory.openSession();
}
}
}
对webapp的所有请求都通过OpenEntityManagerInViewFilter。
在第一个请求中,调用DAO使用简单的Criteria从数据库中获取一些配置:
@Override
public ApplicationSettings getSettings()
{
Criteria criteria = getSession().createCriteria(ApplicationSettings.class);
return (ApplicationSettings) criteria.uniqueResult();
}
此调用导致带有消息的HibernateException:如果没有活动事务,createCriteria无效。
我在这里可以错过什么?