简体   繁体   English

Spring / JPA / Hibernate持久化实体:没有任何事情发生

[英]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 ! 我正在尝试用Spring 3,JPA 2和Hibernate 3创建一个应用程序。当y持久化实体时我有一个问题:什么都没发生! 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. 但是,当我使用query.getResultList()之类的请求时,select正常工作。

So I think my problem is only on a persist/update and on the transaction manager but i'm not really good with spring. 所以我认为我的问题只出现在持续/更新和事务管理器上,但我对spring并不是很好。 Can you help me please ? 你能帮我吗 ?

Here are my configuration files : 这是我的配置文件:

My applicationContext.xml 我的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.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. 在我的情况下,我需要在我的dispacher-servlet.xml中添加以下行。 So I need this in 2 places (applicationContex.xml and dispacher-servlet.xml) 所以我需要在2个地方(applicationContex.xml和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. 为了清除某些内容,你没有显示你的服务方法“存储”对象,但我相信它是用@Transactional注释的 - 因为没有你要创建的新事务。

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. 问题是当你说一个事务性问题时,将@Transactional注释添加到更新数据库的所有方法。 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. 然后将CGLIB库添加到您的calsspath中,或者如果您使用的是Maven,则将其添加到pom.xml中,这对于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. 这是我的db.xml,其中包含所有与数据库相关的spring配置。

<!-- 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.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. 您需要将模型对象添加到persistence.xml文件中。 Right below the provider element add 在提供者元素下方添加

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SPRING JPA-org.hibernate.PersistentObjectException:传递给持久化的分离实体: - SPRING JPA - org.hibernate.PersistentObjectException: detached entity passed to persist: Spring 数据 JPA 和 hibernate 分离的实体传递以保持多对多关系 - Spring data JPA and hibernate detached entity passed to persist on ManyToMany relationship Hibernate + JPA + spring boot org.hibernate.PersistentObjectException:分离的实体传递给持久化 - Hibernate + JPA + spring boot org.hibernate.PersistentObjectException: detached entity passed to persist Hibernate JPA:传递给持久化的分离实体 - Hibernate JPA: detached entity passed to persist Spring + JPA + Hibernate:persist正在令人惊讶地更新实体。 请详细说明 - Spring+JPA+Hibernate: persist is updating the entity surprisingly. Please go through the details 无法在JPA + Spring中持久化实体,没有错误 - Not able to Persist entity in JPA + Spring, No error 无法在春季3中在JPA中保留实体 - not being able to persist entity in JPA in spring 3 Spring,Hibernate,JPA - 为什么我不使用实体管理器,为什么 casacdeType.Persist 只适用于实体管理器 - Spring, Hibernate, JPA - Why am i not using entity manager, why does casacdeType.Persist only work with entity manager 删除然后在JPA / Hibernate中查询失败(已删除的实体传递给持久化) - Remove then Query fails in JPA/Hibernate (deleted entity passed to persist) Java-Jpa-Hibernate独立实体在合并时持续存在 - Java-Jpa-Hibernate Detached entity pases to persist on merge
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM