I am using container managed transactions in my JavaEE application, but, as I have understood it, container managed entitymanagers lacks support for batch inserts. And now I have a case where I will insert a lot of data into the DB. Is it possible, in some way, to combine a container managed entitymanager with an application managed entitymanager in a bean?
If so, I could make a method in my bean that commits the data after I have called entitymanager.persist(myEntity);
several times, making it a batch insert.
But to get that working, I now have to set @TransactionManagement(TransactionManagementType.BEAN)
for the whole class/bean, making the whole bean application managed. But I want my other methods to be container managed, just one method (the method making batch inserts) to be application managed.
Is that possible or are there any other approaches for cases like this?
JDBC batching is a cross-cutting concern and you can get it working for all entity manager configurations.
First you need to set the following Hibernate properties:
<property name="hibernate.order_updates" value="true"/>
<property name="hibernate.order_inserts" value="true"/>
<property name="hibernate.jdbc.batch_versioned_data" value="true"/>
<property name="hibernate.jdbc.fetch_size" value="20"/>
<property name="hibernate.jdbc.batch_size" value="50"/>
Also make sure you use SEQUENCE or TABLE identifier generators since IDENTITY will disable JDBC batching