简体   繁体   中英

Spring/JPA/Hibernate persist entity : Nothing happen

I'm trying to make an application with Spring 3, JPA 2 and Hibernate 3. I have a problem when y persist an entity : nothing happen ! Data are not inserted in database, and not query is executed. But when i'm using a request like query.getResultList() a select works correctly.

So I think my problem is only on a persist/update and on the transaction manager but i'm not really good with spring. Can you help me please ?

Here are my configuration files :

My applicationContext.xml

    <jee:jndi-lookup id="soireeentreamis_DS" jndi-name="jdbc/soireeentreamis" />

    <bean id="persistenceUnitManager"
        class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence.xml</value>
            </list>
        </property>
        <property name="defaultDataSource" ref="soireeentreamis_DS" />
        <property name="dataSources">
            <map>
                <entry key="soireeentreamisDS" value-ref="soireeentreamis_DS" />
            </map>
        </property>
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />
        <property name="persistenceUnitName" value="soireeentreamisPU" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
            </bean>
        </property>
    </bean>

    <bean id="soireeentreamisTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="soireeentreamisTransactionManager" />

    <context:annotation-config />
</beans>

My persistence.xml

    <persistence-unit name="soireeentreamisPU"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>soireeentreamisDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create"/>
        </properties>
    </persistence-unit>

My service

 @Service @Transactional("soireeentreamisTransactionManager") public class UserServiceImpl implements UserService ... 

My dao

 @Repository public class UserDaoImpl extends GenericDaoImpl<User, String> implements UserDao { @PersistenceContext(unitName = "soireeentreamisPU") private EntityManager em; public void persist(final User entity) { em.persist(entity); } } 

Can somebody help me please?

I find similar problem a while ago. In my case I needed to add line below to my dispacher-servlet.xml. So I need this in 2 places (applicationContex.xml and dispacher-servlet.xml)

<tx:annotation-driven />

And to clear something out, you didn't show your service methode that "store" object, but I believe that it's annotated with @Transactional - cause wihout this one you want create new transaction.

This happened to me recently. The problem is as you say a transactional problem, add the @Transactional annotation to all the methods that update the DB. Then add the CGLIB library to your calsspath or add it to your pom.xml if you are using Maven, this is necessary for spring make transactions. Even if I did it on a different way I hope it can help you. Here is my db.xml where I have all the data base related spring configuration.

<!-- Scans within the base package of the application for @Components to configure as beans -->

<context:property-placeholder location="classpath:db.properties"/>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <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="${db.dialect}" />
        </bean>
    </property>
</bean>

<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
    destroy-method="close">
    <property name="driverClass" value="${db.driver}" />
    <property name="jdbcUrl" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
    <property name="idleConnectionTestPeriodInMinutes" value="1" />
    <property name="idleMaxAgeInMinutes" value="4" />
    <property name="maxConnectionsPerPartition" value="30" />
    <property name="minConnectionsPerPartition" value="10" />
    <property name="partitionCount" value="3" />
    <property name="acquireIncrement" value="5" />
    <property name="statementsCacheSize" value="100" />
    <property name="releaseHelperThreads" value="3" />
</bean>

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="jpaDialect" ref="jpaDialect"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

And here is my persistence.xml

<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="pu-app" transaction-type="RESOURCE_LOCAL">


</persistence-unit>

You need to add the model object into the persistence.xml file. Right below the provider element add

<class>com.your.domain.object.User</class>

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