【问题标题】:JPA .persist() doesn't save the data to the database while read works properlyJPA .persist() 在读取正常工作时不会将数据保存到数据库中
【发布时间】:2013-10-24 04:28:50
【问题描述】:

我有一个 Spring/JPA(Hibernate 作为 JPA 提供者), MySQL 作为数据库提供者。 即使我可以从数据库中读取数据,我似乎也无法成功持久化数据。

我已经搜索了 stackoverflow,并且大多数解决方案都涉及使用 @Transactional(也是类)装饰 save 方法。我验证了整体配置,但似乎找不到哪里出错了。

这是我的配置

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/ p   ersistence/persistence_2_0.xsd"
             version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="brPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:comp/env/jdbc/brDS</jta-data-source>
        <class>com.uhsarp.br.domain.Bill</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
              <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
        </properties>
    </persistence-unit>
</persistence>

spring 应用上下文

   <?xml version="1.0" encoding="UTF-8" standalone="no"?>
   <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans-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/tx
                          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   <!-- JPA Entity Manager Factory -->
 <!--    <bean id="entityManagerFactory" 
         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
         p:dataSource-ref="brDS"/>-->

         <bean id="entityManagerFactory" 
 class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="brPersistenceUnit"/> 
  </bean> 
   <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

   <!-- Database LOB Handling -->
   <bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />

   <!-- Read in DAOs from the JPA package -->
   <context:component-scan base-package="com.uhsarp.br.dao.framework.impl" />

   <!-- Transaction Config -->
   <bean id="transactionManager"
         class="org.springframework.orm.jpa.JpaTransactionManager"
         p:entityManagerFactory-ref="entityManagerFactory"/>
 <tx:annotation-driven/>
</beans>

DAO类的sn-p

 @Repository("billDAO")
 @Transactional
 public class BillDAOImpl  implements BillDAO{
  @PersistenceContext
  private EntityManager em;

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
 public Bill save(Bill bill) {
     if (bill.getId() == null) {
                     em.persist(bill);

        return bill;
    } else {
        return em.merge(bill);
    }
 }
  }

【问题讨论】:

  • 你的问题没有意义;如果您能够读取数据库中的数据,则它已被持久化。
  • 我能够读取的数据没有被 JPA 持久化。
  • 您似乎没有为您的 EntityManagerFactory 提供任何数据源(在 spring config 和 persistence.xml 中都没有),因此不清楚您的代码如何连接到底层数据库。
  • @SergeyMakarov 数据源被配置为应用服务器上的一个池。我通过 jndi 密钥“brDS”连接到它。我已经编辑了 persistence.xml 来展示它(我只是错过了将它包含在原始帖子中)。谢谢。
  • 同意mrak。我怀疑事务根本不起作用,@transactional 在这里被忽略,否则会发布错误消息。还要发布您用来确定这不起作用的实际代码!如果您发现了问题,请将解决方案作为答案发布。

标签: java sql spring jpa dao


【解决方案1】:

问题不在于 JPA。

我有来自前端的数据(作为 JSON)。 Spring MVC 完成了数据绑定并创建了“Bill”对象。即使对象结构的形成没有显示任何错误,id 也没有正确填充,这使得 JPA 无法通过 .persist 查询。

休眠日志(persistence.sql 中的 show_sql)确实显示事务正在回滚。但是休眠日志是我遇到过的最糟糕的日志之一(或者我可能会失明)。它们非常冗长,很难找到错误。不过,我承认我不是 Hibernate 的老手。

我面临的另一个挑战是我分别设计了数据库和对象,并尝试使用表来验证对象(其他方法是让 Hibernate 生成表或让它从表中生成实体)。虽然这无论如何都不是一个坏习惯,但重要的是要确保数据库和实体的设计是正确和合法的,这样一切才能正常工作。应用程序可能构建得很好,JPA 可能会验证表,但这并不一定意味着您的数据模型清晰易读。我在 JPA 的最初冒险中学到了一些艰难的经历,但我感谢 Stackoverflow 的开发人员在这个过程中的智慧。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2018-08-25
    • 2011-01-19
    • 1970-01-01
    • 2022-01-12
    • 2017-11-24
    相关资源
    最近更新 更多