我将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之类的注释创建的 - 所以我没有看到任何意义,为什么我应该遇到这样的问题。
答案 0 :(得分:2)
@Transactional
是Spring的一部分。您的代码似乎未使用Spring context
。
要使用ImageRepository.factory()
方法,需要通过Spring上下文包装才能使@Transactional
注释生效。