I am newbie to Hibernate framework and I am curious about the way persist and update work.
Currently in my project when I would like to persist or update collection of data into database, I am doing it one by one via looping method. For instance,
public persistData(){
List<Person> personList = new ArrayList<>();
for(Person person : personList){
session.persist(person);
}
}
Is it possible to, for example,
session.persist(personList);
Or there is anyway else I can persist/update collection of data at once without looping?
Editted: I have found Hibernate Batch Processing in How to insert multiple rows into database using hibernate? and Best way to insert a good amount of records in hibernate
I am developing generic class for persist/update/delete data with hibernate, should I provide the method with
public void (List<T> addedItemList)
or
public void (T addedItem)
For my understanding, bulk persist should be done with large amount of transactions right? If some times there is only 1 or 2 objects to be persisted, is batch processing still appropriate?
The main point here is to begin the transaction, persist all, and then commit. Try not using the @Transaction since you are doing it with the code.
EntityManager em = entityManagerProvider.get();
// em.clear();//remove this if caching is necessary
em.getTransaction().begin();
for(buildings b:buildingsArray) {
em.persist(b);
}
em.getTransaction().commit();
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.