1

I have an Entity with ManyToMany relation

@Entity
@Table(name = "my_table")
public class Entity implements Persistable<String> {

    @Id
    private String name;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "join_entity",
            joinColumns = @JoinColumn(name = "name",foreignKey = @ForeignKey(name = "link",foreignKeyDefinition = " FOREIGN KEY (name) REFERENCES roles(name) on delete cascade")),
            inverseJoinColumns = @JoinColumn(name = "s_name",foreignKey = @ForeignKey(name = "priLink",foreignKeyDefinition = " foreign key (s_name) REFERENCES entity_2(s_name) on delete cascade")))
    private Set<Entity2> s_entity = new HashSet<>();

// getters and setters
}

i'm using PostgresSQL 15.0, and have enabled rewritebatchedinserts property (note batch is enabled)

spring.datasource.url=jdbc:postgresql://localhost:5432/myDb?reWriteBatchedInserts=true

#JPA logs
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.stat=DEBUG
spring.jpa.properties.hibernate.generate_statistics=true

When I try to saveAll for saving a size of 2 Entity and each Entity containing 2 instance of Entity2 I end up with 6 insert query (from the logs) and

when I try inserting 1000 rows with and without rewritebatchedinserts option, there is hardly any difference in time with former taking 270ms and latter 274ms on average.

based on these articles I was expecting lot more

baeldung
vladmihalcea
dzone
How to do bulk (multi row) inserts with JpaRepository?

why is it behaving this way and how can I make it work(if it's not working)?

Akash Jain
  • 684
  • 9
  • 23

0 Answers0