The core concept of any ORM framework is that your classes reflect the state of the database at the current time. This means that you need to synchronize your JVM data with the database at certain times. By default, Hibernate and JPA use FlushMode.AUTO
, which results in a synchronization before every HQL, SQL, or criteria query.
This guarantees that your classes' data will always reflect the content of the database but it is detrimental for performance since DML like UPDATE
, INSERT
, and DELETE
operations execute in addition to a SELECT
for the query.
For example, consider the following EJB method, which performs a set of transactions to finalize an order:
public Long customerTransaction(Long customerId, Long itemId){ Item item = em.find(Item.class, itemId); Customer customer = em.find(Customer.class,customerId); item.setCustomer(customer); customer.addOrder(item); Query q1 = em.createNamedQuery("findCustomer"); q.setParameter("Id", customerId);...