spring jpa hibernate写入数据库失败 - 没有事务

时间:2011-12-10 14:40:46

标签: java mysql hibernate spring jpa

我有以下问题。我使用Spring和JPA(Hibernate)将数据保存在数据库中。
但是在保存数据时出错。创建新用户后,我的数据库保持空白。以下是重要文件:

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

我认为它是一个传统错误。 我已经花了很多时间在其他渠道。什么意思是“由于没有正在进行的交易而延迟身份插入”?

事先感谢你的帮助 问候语

1 个答案:

答案 0 :(得分:4)

问题是UserDaoImpl中的@Repository,删除它并创建一个bean

<bean id="userDao" class="de.bht.swp.lao.ocp.user.UserDaoImpl" />

在Context.xml中

我无法解释这种行为,但这就是原因。