我在基于Spring 3.2.15的应用程序中将Hibernate从3.6升级到4.2.21。
这是我的配置
@Configuration
@ComponentScan(basePackageClasses = { myClass.class })
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableTransactionManagement
@EnableCaching
public class JavaConfigSpring {
// ############ SESSION FACTORY ############ //
@Bean(name = "sessionFactory")
@Autowired
@Primary
public LocalSessionFactoryBean localSessionFactoryBean() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
return localSessionFactoryBean;
}
// ############ TRANSACTION MANAGER ############ //
@Bean(name = "transactionManager")
@Autowired
@Primary
public HibernateTransactionManager hibernateTransactionManager(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
txManager.setRollbackOnCommitFailure(true);
return txManager;
}
@Bean
public BeanPostProcessor persistenceTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
和我的hibernate配置文件:
<hibernate-configuration>
<session-factory>
<property name="show_sql">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pass</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@xxx.xx.xxx.xx:xxx:MyBase</property>
<property name="hibernate.default_batch_fetch_size">10</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<!--Pool C3PO-->
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.idle_test_period">120</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_size">2</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.min_size">2</property>
<mapping resource="com/toto/persistence/hibernate/hbm/Profiles.hbm.xml"/>
<mapping resource="com/toto/persistence/hibernate/hbm/Role.hbm.xml"/>
</session-factory>
我还将@Transactional放在正确的位置,这是错误发生的地方
@Component
@Transactional
public class LabelsFactory {
@Autowired
SessionFactory sessionFactory;
public List<Labels> getLabels() {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Labels.class);
return crit.list();
}
}
但是当我启动应用程序时出现了这个错误:
org.hibernate.HibernateException:没有活动事务,createCriteria无效
显然交易经理没有工作
我试图删除:
<property name="hibernate.current_session_context_class">thread</property>
但后来我遇到了这个新错误:
org.hibernate.HibernateException:找不到当前线程的会话
我的配置有什么问题?