【发布时间】:2012-04-03 01:37:50
【问题描述】:
我使用 Hibernate 和 JPA。我有一个用 @Transactional 注释的类,在这个类中我保留了一些对象:
@Transactional
@Repository
public class ImageRepository {
@Autowired
private EntityManager em;
public Image factory(String imageHash) {
Image image = new Image();
image.setHash(imageHash);
em.persist(image);
return image;
}
}
我可以看到 hibernate 用select nextval ('hibernate_sequence') 为图像对象分配了 ID。但仅此而已。该对象从未放入数据库中,当我尝试运行 em.flush(); 时,我得到:
javax.persistence.TransactionRequiredException: no transaction is in progress
我也没有收到任何错误。
我的应用程序上下文:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:annotation-config/>
<context:spring-configured/>
<context:property-placeholder location="classpath:application.properties"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${database.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${database.structure}</prop>
<prop key="hibernate.connection.url">${database.connection}</prop>
<prop key="hibernate.connection.username">${database.username}</prop>
<prop key="hibernate.connection.password">${database.password}</prop>
<prop key="hibernate.connection.driver_class">${database.driver}</prop>
<prop key="hibernate.connection.shutdown">true</prop>
<prop key="hibernate.connection.writedelay">0</prop>
<prop key="hibernate.show_sql">${database.show_sql}</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
还有persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence>
当我尝试合并一些对象时也会发生同样的情况。
谢谢!
编辑 3:
(以前的编辑不相关)
我注意到,当我从 DWR (DirectWebRemoting) Servlet 持久化或编辑对象时,我没有任何问题。当我尝试从我自己的 Servlet 中做同样的事情时,我提到了一些问题。但是我的 Servlet 的所有 bean 都是由 Spring 通过组件扫描创建的,并带有 @Component 或 @Repository 之类的注释 - 所以我看不出任何意义,为什么我应该遇到这些问题。
【问题讨论】:
标签: java hibernate jpa transactions