Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 825
user1838850 Avatar asked Nov 20 '12 13:11

user1838850


1 Answers

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.

like image 183
greendraco Avatar answered Nov 15 '22 00:11

greendraco