1
我有以下問題。我使用Spring和JPA(Hibernate)將數據保存在數據庫中。
但我在保存數據時發生錯誤。創建新用戶後,我的數據庫保持空白。這裏有重要的文件:春季jpa休眠寫入數據庫失敗 - 無事務
UserDao Interface:
import java.util.List;
public interface UserDao {
public User findById(Integer id);
public List<User> findAll();
public User findByEmail(String email);
public void save(User user);
}
在UserDAOImpl:
@Repository
public class UserDaoImpl implements UserDao {
@PersistenceContext
private EntityManager em;
@Override
public User findById(Integer id) {
return em.find(User.class, id);
}
@SuppressWarnings("unchecked")
@Override
public List<User> findAll() {
return (List<User>)em.createQuery("from User u").getResultList();
}
@Override
public User findByEmail(String email) {
User user = null;
try
{
user = (User)em.createQuery("from User u where u.email = ?1").setParameter(1, email).getSingleResult();
}
catch(NoResultException e){}
return user;
}
@Override
@Transactional
public void save(User user) {
em.persist(user);
}
}
的context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" default-autowire="byName">
<context:component-scan base-package="de.bht.swp.lao.ocp" />
<context:annotation-config />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="root" />
<property name="password" value="root" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/ocp" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="ocpPU" />
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</bean>
</property>
<property name="loadTimeWeaver" ref="loadTimeWeaver"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<context:load-time-weaver weaver- class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</beans>
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="ocpPU">
</persistence-unit>
當我創建一個新的用戶我得到以下錯誤日誌:
14:42:05,703 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - delaying identity-insert due to no transaction in progress
14:42:05,704 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Closing JPA EntityManager
14:42:05,707 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Rendering view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [/user/login.htm]] in DispatcherServlet with name 'dispatcher'
14:42:05,708 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request
我認爲這是一個錯誤Trancation。 我已經花了很多時間在其他渠道。什麼意思是「由於沒有進行交易而延遲身份認證」?
感謝您的幫助提前 問候
thx很多,解決了問題 – ChristianB
經過一些版本升級後,我們開始在特定環境下的後臺任務中遇到此錯誤。將'@ Repository'切換到'@ Component'解決了這個問題。如果你能詳細說明會很好。 –