Is there a way that i can do batch insert in db using hibernate while using GenerationType as Identity, kind of I don’t want the persist method to go to database for immediate insert, kind of lazy persist is it possible, also is there a way to persists lots of entities at once using one db network call only in case of using Identity???
org.hibernate.StatelessSession#insertMultiple
allows to insert multiple entities at once, but an optimization to use JDBC batching for identity inserts has not been implemented yet. In theory, the stateful Session
could also offer a persistMultiple
method with similar semantics, but so far, nobody stepped up to implement this yet.
I can’t use the Stateless session since I need to use L1 cache there, so lets say I have 1 million queries which are build through createNativeQuery can we execute them all at once, because we can do similar thing in mysql using preparedStatement, like creating a preparedStatement and adding it to batch and then at last execute the batch, can I achieve similar thing here???
You can’t right now if the entity that you are persisting uses IDENTITY
generation. Maybe a future version of Hibernate ORM will offer this.
and statelessSession insertMultiple you said for identity it has not been implemented yet right? so any other alternative that hibernate supports maybe?? and also is it the same case with batch update?
Like I wrote, the optimization has not been implemented yet, but if someone steps up and does the implementation, you would benefit from it by using StatelessSession#insertMultiple
.
There are no alternatives, other than writing the insert statements directly with JDBC batching.
Batch updates will just work fine with entities. The IDENTITY
strategy does not play a part there.