简体   繁体   中英

spring jpa hibernate writing to the DB fails - no transaction

i have the following problem. I use Spring and JPA (Hibernate) to persist Data in a Database.
But i have an Error during saving the Data. My Database keeps empty after I create a new User. Here are the important files:

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>

When i create a new User i get the following Errorlog:

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

I think its a Trancation Error. I´v already spent so much time in other channels. What means "delaying identity-insert due to no transaction in progress"?

thanks for the help beforehand greeting

The problem is the @Repository in UserDaoImpl, remove it and make a bean

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

in your Context.xml

I can't explain this behavior but this is the reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM