[英]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.