I downoladed hibernate-test-case-templates and write test case for hibernate 5.2.12. It works without errors.
Then I update my project to use hibernate 5.2.12, there are no errors too.
But then i rollback to hibernate 5.1.10.
As i use spring data jpa i decide to write interface method in my repository:
void deleteByCredIdIn(List<Long> ids);
and let spring automatically implement it. And it works without errors.
I look at debug window and found that spring jpa use another approach for bulk deletion. It make select with in clause and then delete entities one by one.
As i see there is no problem in 5.2.12, but I wonder if this problem can't reapear in future versions?
My test code for this issue:
Code:
package by;
import javax.persistence.*;
@javax.persistence.Entity
@Access( AccessType.FIELD )
public class Entity {
@Id
@Column(name = "id")
protected Long id;
protected Entity() {
}
public Entity(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
Code:
package org.hibernate.bugs;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.Root;
import by.Entity;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API.
*/
public class JPAUnitTestCase {
private EntityManagerFactory entityManagerFactory;
@Before
public void init() {
entityManagerFactory = Persistence.createEntityManagerFactory( "templatePU" );
}
@After
public void destroy() {
entityManagerFactory.close();
}
// Entities are auto-discovered, so just add them anywhere on class-path
// Add your tests, using standard JUnit.
@Test
public void hhh123Test() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(new Entity(1L));
entityManager.persist(new Entity(2L));
entityManager.persist(new Entity(3L));
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
List<Long> idsForDeletion = new ArrayList<>(Arrays.asList(1L, 2L ));
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaDelete<Entity> query = cb.createCriteriaDelete(Entity.class);
Root root = query.from(Entity.class);
query.where(root.get("id").in(idsForDeletion));
entityManager.createQuery(query).executeUpdate();
entityManager.getTransaction().commit();
entityManager.close();
}
}