简体   繁体   English

Spring Data JPA + Hibernate会执行保存方法,但不会执行插入/更新语句

[英]Spring Data JPA + Hibernate do execute save methods, but don't execute insert/update statements

Issue 问题

The problem is that, at each build of the project with Maven, the build produces randomly a working jar or a broken one. 问题在于,在使用Maven进行的项目的每个构建中,该构建都会随机生成一个工作jar或一个损坏的jar。 Obviously there aren't any changes neither in code or configuration. 显然,代码或配置都没有任何改变。

What I mean with broken? 我的意思是坏了吗?

  • Broken Jar. 罐子破了。 The Job starts and ends correctly, without exceptions, without rollback of any kind, and with all save methods of CrudRepository executed just fine. Job正确地开始和结束,没有异常,没有任何类型的回滚,并且执行了CrudRepository的所有保存方法都很好。 The problem is that even if I see in the log the select statements to fetch the next value of the sequences for the inserts, the inserts are never logged. 问题是,即使我在日志中看到选择语句以获取插入序列的下一个值,也不会记录插入。 Checking the DB, the inserts are not only not logged, but actually they are all never executed. 检查数据库时,不仅不记录插入,而且实际上从未执行过所有插入。 In fact, the corresponding tables remain empty! 实际上,对应的表仍然为空! One more important detail is that the entities passed to the CrudRepository save methods have the IDs set. 一个更重要的细节是,传递给CrudRepository保存方法的实体具有ID集。 This is the case for all save/updates, and not only for some of them. 所有保存/更新都是这种情况,而不仅仅是其中一些。
  • Working JAR. 工作JAR。 Both select and insert statements are logged correctly and record are inserted in tables. select和insert语句均正确记录,并将记录插入表中。

Usually, each 5/6 builds, 1 working JAR is generated, while 4/5 are broken. 通常,每5/6个构建,生成1个有效的JAR,而4/5被破坏。 The broken and working jars are identical with binary comparison. 破损的罐子和工作罐子与二进制比较相同。

Once a JAR is compiled, if it is broken, each execution will be broken, regardless of how many times it is executed. 编译JAR后,如果JAR被破坏,则每次执行都会被破坏,无论它执行了多少次。 Same goes for the working one. 工作的人也是如此。

What I Already Tried 我已经尝试过的

  1. Changing Spring/Hibernate versions 更改Spring / Hibernate版本
  2. Compiling on different machines 在不同的机器上编译
  3. Replacing CrudRepository with JpaRepository 用JpaRepository替换CrudRepository
  4. Forcing commit with saveAndFlush (resulting in the generation of an exception) 使用saveAndFlush强制提交(导致生成异常)

Configuration 组态

I have a Spring Batch Project, based on Spring Data JPA and Hibernate. 我有一个基于Spring Data JPA和Hibernate的Spring Batch项目。 The batch is executed by a sh file. 批处理由sh文件执行。 As you can see in the configuration, for debug reasons, I have hibernate.show_sql parameter set to true. 如您在配置中看到的,出于调试原因,我将hibernate.show_sql参数设置为true。 The database is Oracle 11g. 该数据库是Oracle 11g。

POM: POM:

    <properties>
         <spring-version>5.1.5.RELEASE</spring-version>
        <hibernate-version>5.4.1.Final</hibernate-version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate-version}</version>
        </dependency>
    .....
  </dependencies>

Hibernate Properties: 休眠属性:

hibernate.synonyms=true
hibernate.allocationSize=1000
hibernate.order_inserts=true
hibernate.order_updates=true
hibernate.show_sql=true
hibernate.jdbc.batch_versioned_data=true
hibernate.id.new_generator_mappings=true
hibernate.jdbc.batch_size=30
hibernate.generate_statistics=false

Working Log: 工作日志:

2019-06-27 10:42:03.558 [pool-3-thread-19] INFO  i.a.n.b.t.XXX (246) - Elaborazione TABLE1 con SEQU: 2071042
Hibernate: select SEQUENCE1.nextval from dual
Hibernate: insert into TABLE2 (........) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select column1, column2 ... from TABLE3  where condtion
2019-06-27 10:42:03.651 [pool-3-thread-19] INFO  i.a.n.b.s.i.BaseOrderServiceImpl (185) - Recuperato record dalla TABLE3 con ID : 2071042

Broken Log: 日志损坏:

2019-06-27 10:42:03.558 [pool-3-thread-19] INFO  i.a.n.b.t.XXX (246) - Elaborazione TABLE1 con SEQU: 2071042
Hibernate: select SEQUENCE1.nextval from dual

INSERT MISSING HERE

Hibernate: select column1, column2 ... from TABLE3  where condtion
2019-06-27 10:42:03.651 [pool-3-thread-19] INFO  i.a.n.b.s.i.BaseOrderServiceImpl (185) - Recuperato record dalla TABLE3 con ID : 2071042

Call for help: 寻求帮助:

I am building my project multiple times each time I need to deploy it, for months. 每次需要部署该项目时,我都会多次构建该项目,几个月。 I'd like to solve this problem once and for all and I'd appreciate your help. 我想一劳永逸地解决这个问题,感谢您的帮助。

If you need snippet of codes ask me, but generally they are normal save with basic entities, with @Transactional notation working fine with working JAR. 如果您需要一小段代码,请问我,但通常来说,除了基本实体外,它们都是正常的,@ @Transactional表示法可以与工作JAR一起正常工作。

Thank you. 谢谢。

Update 更新资料

As suggested I tried to uniform spring and hibernate dependencies to the same version. 如建议的那样,我尝试将spring和hibernate依赖关系统一到同一版本。 The problem is still there. 问题仍然存在。

Update pom 更新pom

 <properties>
    <spring-version>5.0.12.RELEASE</spring-version>
    <hibernate-version>5.2.17.Final</hibernate-version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.1.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.17.Final</version>
    </dependency>
</dependencies>

I resolved the issue with the following steps: 我通过以下步骤解决了该问题:

  • Versions revion suggested by @bdshadow. @bdshadow建议的版本修订。
  • I had a @Configiguration class, with @EnableJpaRepositories, with entityManagerFactoryRef and transactionManager refering to bean defined in this class. 我有一个@Configiguration类,其中包含@EnableJpaRepositories,其中的EntityManagerFactoryRef和transactionManager引用了此类中定义的 bean。 The beans had the default qualifiers entityManagerFactory and transactionManager . Bean具有默认的限定符entityManagerFactorytransactionManager I changed the qualifiers and added the notation @Primary to them. 我更改了限定词,并在它们中添加了@Primary表示法。

These steps resolved the issue. 这些步骤解决了该问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM