I'm using JPA with Hibernate and spring-boot-starter-data-jpa. I want to merge/update a set of Items. If it's a new item, I want to persist, if it's an already existsing item i want to update it.
@PersistenceContext(unitName = "itemEntityManager")
private EntityManager em;
@Transactional
public void saveItems(Set<Item>> items) {
items.forEach(em::merge);
}
When I try it like this, every Item creates a new HQL statment and it's inperformant. So I'm looking for a way to save all Items in one time (if it's possible), to save calls and time.
I found this:
EntityTransaction transaction = em.getTransaction();
transaction.begin();
items.forEach(em::merge);
transaction.commit();
but i can't use this transaction because i use the @Transactional. Is there a way with native SQL?