我創建了一個基本的Spring MVC/JPA/Hibernate應用程序。我正在嘗試保存一個UserProfile實體來測試是否可以實際保留它,但沒有保存,也沒有引發異常。Spring JPA不存儲實體
在控制器方法中,我創建了一個簡單的UserProfile(這是一個@Entity),然後我將它發送給一個服務方法。 UserProfileServiceImpl類用@Service註釋,addUserProfile(UserProfile配置文件)方法用@Transactional註釋。
在服務方法中,我所做的只是調用一個DAO方法(用@Repository註解的類)。在DAO方法中,我所做的就是調用entityManager.persist(object),其中object是用戶配置文件對象。
- 沒有寫入服務器日誌,日誌級別位於INFO。
- 沒有出現在Mysql查詢日誌中(我知道查詢日誌有效)
- entityManager得到正確的注入。
- 數據源正確啓動,因爲當我輸入錯誤的憑據時,我得到了SQLExceptions。
我希望你能告訴我什麼是錯的。我將在下面發佈我的一些代碼和配置文件。
服務方法:
// The service method gets called from the controller.
// Its class is annotated with @Service
@Transactional(readOnly = false)
public void addUserProfile(UserProfile userProfile) {
userProfileDao.save(userProfile);
}
在DAO方法:
// The save(T object) method is in the GenericDaoJpa class, which is the superclass
// of the UserProfileDaoJPA class that is referenced from the service.
// I have established that the entityManager is there and the object is a
// UserProfile. The @Repository annotation is on the child class UserProfileDaoJpa.
public void save(T object) {
entityManager.persist(object);
}
主要應用-context.xml中
<context:property-placeholder location="classpath*:**/*.properties"/>
<import resource="spring-jpa.xml"/>
於應用程序的上下文web.xml文件
<mvc:annotation-driven />
<context:component-scan base-package="nl.codebasesoftware.produx" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
彈簧jpa.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${db.driverClassName}"
p:url="${db.url}" p:username="${db.username}" p:password="${db.password}"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
的persistence.xml
<persistence-unit name="mysqlPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
<!-- Needed to properly process @PersistenceContext -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
不知何故沒有SQL被髮送到MySQL這種設置,但也不例外要麼拋出,所以我不知道這是怎麼回事上。希望你能幫助:)
其中休眠和你正在使用Spring版本? – 2012-07-18 05:55:02
Spring 3.1.1和Hibernate 4.1.4 – Julius 2012-07-18 09:42:06
剛纔我注意到,當我在DAO方法中的entityManager.persist()之後調用entityManager.flush()時,我得到以下錯誤: - 請求處理失敗;嵌套異常是javax.persistence.TransactionRequiredException:沒有事務正在進行。顯然沒有交易.... – Julius 2012-07-18 09:49:15