简体   繁体   中英

Hibernate+ postgres batch update does not work

Is there any way to do batch updates ?

i create a simple entity

@Entity
@Getter
@Setter
public class A {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "A_ID_GENERATOR")
  @SequenceGenerator(name = "A_ID_GENERATOR", sequenceName = "a_id_seq")
  private Long id;

  private String name;
}

next step i generated 10000 objects of class A and put them to db

next step i get list of A from db ,set new name and save them again

  @PutMapping
  @Transactional
  public String updateAllTest(){
    var list=aRepository.findAll();

    for (int i = 0; i <list.size() ; i++) {
      list.get(i).setName("AA"+i);

    }
    return "OK";
  }

what did i expect- i expect that hibernate will do batch update and hibernate did it -hibernate statistics says - it execute 200 batches ( batch size=500)

next i go to db log files and what i see there- there are no batches- only single updates -10 000 rows

it looks like same with batch insert without adding reWriteBatchedInserts=true to JDBC driver

so is there any way to do batch updates in postgres with hibernate or no?

The key thing to understand is that reusing the same server side statement handle for multiple executions is batching. The log is just telling you about every execution, but that doesn't mean it is slow. It's doing exactly what it should do.

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