简体   繁体   中英

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. 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. 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. This is the case for all save/updates, and not only for some of them.
  • Working JAR. Both select and insert statements are logged correctly and record are inserted in tables.

Usually, each 5/6 builds, 1 working JAR is generated, while 4/5 are broken. 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. Same goes for the working one.

What I Already Tried

  1. Changing Spring/Hibernate versions
  2. Compiling on different machines
  3. Replacing CrudRepository with JpaRepository
  4. Forcing commit with saveAndFlush (resulting in the generation of an exception)

Configuration

I have a Spring Batch Project, based on Spring Data JPA and Hibernate. The batch is executed by a sh file. As you can see in the configuration, for debug reasons, I have hibernate.show_sql parameter set to true. The database is Oracle 11g.

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.

Thank you.

Update

As suggested I tried to uniform spring and hibernate dependencies to the same version. The problem is still there.

Update 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.
  • I had a @Configiguration class, with @EnableJpaRepositories, with entityManagerFactoryRef and transactionManager refering to bean defined in this class. The beans had the default qualifiers entityManagerFactory and transactionManager . I changed the qualifiers and added the notation @Primary to them.

These steps resolved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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