我有一个Spring4 Web应用程序。最初我使用的是Hibernate SessionFactory
,并使用Spring Hibernate API进行开发。一切都很好。愚蠢的是,我最近决定转而使用JPA,Hibernate仍然是我的提供者。我重新配置了我的Spring设置并重写了我的大部分代码。最初经过测试,以便我的所有数据库读取都能正常工作。然后我尝试了数据库写入,它们都像这样失败:
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:970)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
at com.sun.proxy.$Proxy47.flush(Unknown Source)
at com.taubler.oversite.dao.impl.EntityDaoImpl.insert(EntityDaoImpl.java:65)
...
请注意,在使用HibernateTemplate
,SessionFactory
,HibernateTransactionManager
等时,我的代码运行正常。我的业务逻辑类以及我的DAO都使用{{1注释就像以前一样。
看起来Hibernate正在尝试创建一个事务,因为我在堆栈跟踪之前的日志中看到以下内容:
@Transactional
这是一些相关的代码。首先,我的spring配置XML片段:
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - initial autocommit status: true
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - disabling autocommit
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.internal.SessionImpl - Opened session at timestamp: 14115372286
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.internal.SessionImpl - Setting flush mode to: AUTO
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.internal.SessionImpl - Setting cache mode to: NORMAL
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.event.internal.AbstractSaveEventListener - Transient instance of: com.taubler.oversite.entities.EmailAddress
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.event.internal.DefaultPersistEventListener - Saving transient instance
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.event.internal.AbstractSaveEventListener - Saving [com.taubler.oversite.entities.EmailAddress#<null>]
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.engine.spi.IdentifierValue - ID unsaved-value: null
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.event.internal.AbstractSaveEventListener - Delaying identity-insert due to no transaction in progress
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.internal.SessionImpl - Opened session at timestamp: 14115372287
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.internal.SessionImpl - Setting flush mode to: AUTO
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.internal.SessionImpl - Setting cache mode to: NORMAL
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.engine.transaction.spi.AbstractTransactionImpl - rolling back
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - rolled JDBC Connection
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - re-enabling autocommit
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl - after transaction completion
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.internal.SessionImpl - after transaction completion
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.internal.SessionImpl - Closing session
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Closing logical connection
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.engine.jdbc.internal.JdbcResourceRegistryImpl - Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcResourceRegistryImpl@2c4e3947]
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Releasing JDBC connection
2014-09-24 05:40:28 [http-bio-8080-exec-3] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Released JDBC connection
2014-09-24 05:40:28 [http-bio-8080-exec-3] TRACE org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - Logical connection closed
示例业务逻辑(经理)类。这个类是<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.taubler.oversite.entities" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
进入SpringMVC控制器,所以Controller正在调用代理:
@Autowired
该经理打电话给DAO:
...
@Autowired
private EmailAddressDao emailAddressDao;
...
@Override
@Transactional
public EmailAddress addEmailAddress(User user, String email) {
EmailAddress emailAddress = new EmailAddress(user, email);
emailAddress.setMain(false);
emailAddress.setValidated(false);
emailAddressDao.insert(emailAddress);
this.initiateEmailValidation(emailAddress);
return emailAddress;
}
我尝试过不同的变体。最初,DAO和Manager方法都使用...
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
protected final EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}
public boolean insert(Entity o) {
o.setCreated(new Date());
this.getEntityManager().persist(o);
this.getEntityManager().flush();
return true;
}
进行了注释,这就是纯Hibernate的工作原理。我尝试删除传播设置,仅注释Manager方法,仅注释DAO方法......没有任何作用。
有什么想法? @Transactional(propagation=REQUIRED);
和HibernateTransactionManager
之间似乎有根本的不同。
答案 0 :(得分:4)
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
protected final EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}
问题是你自己创建了一个实体管理器,不是。只需注入EntityManager
而不是EntityManagerFactory
并使用@PersistenceContext
注释而不是@PersistenceUnit
。 Spring将负责使其成为当前事务的约束。
@PersistenceContext
private EntityManager entityManager;
protected final EntityManager getEntityManager() {
return entityManger;
}
如果您真的想继续注入EntityManagerFactory
,请使用getTransactionalEntityManager
的{{1}}方法来获取Spring托管EntityManagerFactoryUtils
实例。
EntityManager
此外,它与“普通”休眠一起使用并不意味着您的设置必须正确。你提到你使用的protected final EntityManager getEntityManager() {
return EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
}
基本上可以在没有正确的交易设置的情况下工作,因为它会为手头的动作开始新的转换。所以它很可能是应用程序似乎在它实际上没有正确的地方工作。也许你有多个交易,你预期一个(从服务层)。
另一个注意事项是你的代码可能很危险
HibernateTemplate
在您的情况下,这可能会导致创建2个不同的public boolean insert(Entity o) {
o.setCreated(new Date());
this.getEntityManager().persist(o);
this.getEntityManager().flush();
return true;
}
,因此您可能会在持久存储时刷新另一个。接下来,您不应该将EntityManager
称为flush
这将由交易结束完成。
答案 1 :(得分:2)
您需要使用EntityManager
注释直接注入@PersistenceContext
,而不是PersistenceUnit