So, I am using Springboot with Data-JPA with POSTGRESQL to insert data into tables:
@Entity
@Builder
@Data
class Person{
@Id
UUID id;
String name;
}
And the repository for that
@Repository
interface PersonRepository extends JPARepository<Person, UUID>{}
Say we created a List of persons.
List<Person> persons = List.of(....)
Lets assume we have 10 persons in the list..
Now, when I did personRepository.saveAll(persons);
So I got into these problems:
- There were 10 insert statements as well as select select statements before insert.
- I wanted batch insert with size of 5, so I wanted 2 multivalued inserts statements to fire here, but 10 inserts were being fired.
Now, I solved the point 1 with the help of Custom Repository
public interface CustomBaseRepository<T, ID extends Serializable> {
<S extends T> S customInsert(S entity);
<S extends T> Collection<S> customInsertAll(Collection<S> entities);
<S extends T> Collection<S> customSaveAllInBatch(Collection<S> entities);
}
@Transactional
public class CustomBaseRepositoryImpl<T, ID extends Serializable> implements CustomBaseRepository<T, ID> {
@Autowired
private final EntityManager entityManager;
public CustomBaseRepositoryImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public <S extends T> S customInsert(S entity) {
entityManager.persist(entity);
return entity;
}
@Override
public <S extends T> Collection<S> customInsertAll(Collection<S> entities) {
entities.forEach(entityManager::persist);
return entities;
}
@Override
<S extends T> Collection<S> customSaveAllInBatch(Collection<S> entities){
**//TODO(...): HOW TO DO THIS?**
}
}
So I made my custom repository and exposed the EntityManager and m doing a persist, as I know when m saving the 'persons' list, it's all new records.
Now, SELECT before INSERT issue is fixed, but the batch insert issue still persists..
I tried using the personRepository.saveAll(persons) too, it didn't work, i used these commands in the application properties to enable batch as well, but was unable to make it work.
Please help me out on implementing the batch inserts, i.e Insert multiple records in a single insert statement..
I tried using these properties but didn't work:
spring.jpa.properties.hibernate.jdbc.batch_size
spring.jpa.properties.hibernate.jdbc.order_inserts
Also for URL:
reWriteBatchedInserts was enabled.
Please help me out..