I am pretty new to Spring Data JPA and Hibernate and I have the following problem with massive record insertion.
I have the following situation:
1) A RoomMediaDAO Spring Data JPA interface:
@Repository
@Transactional(propagation = Propagation.MANDATORY)
public interface RoomMediaDAO extends JpaRepository<RoomMedia, Long> {
....................................................................
....................................................................
....................................................................
}
2) Into another class I use this RoomMediaDAO to persist a list of RoomMedia object. The list is pretty big and heavy because contains many object and each oof these objects contains a byte[] field representing a BLOB on the mapped table.
So I have the follosing statment that persist the List mediaListToBeInserted:
roomMediaDAO.save(mediaListToBeInserted);
It work fine but it is very slow because it perform the insert one by one, infact in the console I can see something like this:
Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
Hibernate: insert into room_media (description, media, id_room, time_stamp) values (?, ?, ?, ?)
............................................................................
............................................................................
............................................................................
So this is not a good solution.
How can I say to Hibernate to insert more record with a single insert statment? I mean can I do something like this using Hibernate?
INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ),
( Value1, Value2 ),
( Value1, Value2 ),
.................................
.................................
.................................
( Value1, Value2 )
I absolutly need something like this to improve the performance of my batch.