spring-data-jpa icon indicating copy to clipboard operation
spring-data-jpa copied to clipboard

Bug? Repository Batch Insert on BigDecimal not working

Open Isaacwhyuenac opened this issue 3 years ago • 2 comments

Hi I have questions about the saveAll dealing with an entity with the BigDecimal and BigInteger fields.

@Entity
@Data
@IdClass(PTSTemplateEntityPK.class)
@Table(name = "test")
public class TestEntity implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "dpstId")
  private int dpstId;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "arstId")
//  @GeneratedValue(strategy = GenerationType.AUTO)
  private int arstId;

  private BigDecimal name;

  private BigDecimal location;

}

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.domain.PTSTemplateEntityPK;
import com.example.demo.domain.TestEntity;

@Repository
public interface TestRepository extends JpaRepository<TestEntity, PTSTemplateEntityPK> {
}


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.domain.TestEntity;
import com.example.demo.repository.TestRepository;

@RestController
@RequestMapping(value = "/test")
public class TestController {
  private TestRepository testRepository;

  private static List<TestEntity> testEntities = new ArrayList<>();

  static {
    for (int i = 0; i < 2; i++) {
      TestEntity testEntity = new TestEntity();

      testEntity.setLocation(new BigDecimal(i));
      testEntities.add(testEntity);
    }
  }

  public TestController(TestRepository testRepository) {
    this.testRepository = testRepository;
  }

  @GetMapping
  public ResponseEntity<List<TestEntity>> insertData() {
    testRepository.saveAll(testEntities);
    List<TestEntity> all = testRepository.findAll();
    return ResponseEntity.ok().body(all);
  }
}

import java.io.Serializable;

import javax.persistence.Column;

import lombok.Data;

@Data
public class PTSTemplateEntityPK implements Serializable {

  @Column(name = "arstId")
  private int arstId;

  @Column(name = "dpstId")
  private int dpstId;
}

Here I save two items using saveAll to the postgres 14 database using spring-data-jpa-2.7.3 (Java 11).

As you tell I am expecting all 2 items would be saved to the database, but I can see one is saved. If I change the BigDecimal fields to other types like integer/ string (eg private BigDecimal name to private String name), I can see the data insertion is normal (Both items are inserted).

So my questions are:

  1. Is Batch Insert on BigDecimal/ BigInteger type supported?
  2. If it is not supported, what is the recommended way to insert entities with these data types?

Isaacwhyuenac avatar Oct 03 '22 16:10 Isaacwhyuenac

There is a reproducer: https://github.com/Isaacwhyuenac/spring-data-jpa-tester See https://github.com/spring-projects/spring-data-relational/issues/1344#issuecomment-1266508551

schauder avatar Oct 04 '22 08:10 schauder

Is there indication that the problem is caused by Spring Data JPA and not by the underlying JPA implementation (Hibernate I assume)? If so please clarify. If not I suggest to create a reproducer without using Spring and submitting a bug to Hibernate.

The implementation for saveAll is rather simple and I currently have a hard time imagining how it could be at fault for such a behavior.

schauder avatar Oct 04 '22 08:10 schauder

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Oct 11 '22 08:10 spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-projects-issues avatar Oct 18 '22 08:10 spring-projects-issues