I’m going to enable hibernate.order_inserts in order to improve my batch insert operations.
As I can see, this is a property of session factory level. However, I consider enabling such property a little bit risky for all inserts my application do.
Is there an option to enable hibernate.order_inserts property for a single session?
After searching through the hibernate source codes ,when the SessionFactory is built by Configuration.buildSessionFactory() , SettingsFactory.buildSettings() will be called internally to parse the hibernate configuration properties into the Settings instance. The
hibernate.order_insertswill be strode into theorderInsertsEnabledproperty of the Settings instance.The Settings instance then passed to the constructor of the SessionFactoryImpl which is an implementation of the SessionFactory and assign it to its internal settings properties . This settings property is declared to be final which its value cannot be changed once it is set.
The
orderInsertsEnabledproperty of the Settings is exposed to the client through the getter isOrderInsertsEnabled() . Searching the code again and confirm that there is only one place (ActionQueue.sortActions()) to call this getter which the Settings instance is exactly get from the SessionFactoryImplAs a result , I don’t think there have any options to change
hibernate.order_insertsduring run-time no matter it is on the session factory level or session level. So you have to create another session factory withhibernate.order_insertsset to enable to achieve your purpose.