Given the association below, deleting all entities on both sides of the lazy association fails with a constraint violation if (and only if) bytecode enhancement is enabled (e.g. {{<enableLazyInitialization>true</enableLazyInitialization>}} using the {{hibernate-enhance-maven-plugin}}).
{code} @Entity public class Employee { @Id private String name;
@OneToMany(mappedBy = "employee", fetch=FetchType.LAZY) @LazyGroup("Tasks") private List<Task> tasks; ... }
@Entity public class Task { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID_GENERATOR") @SequenceGenerator(name = "ID_GENERATOR", sequenceName = "TASK_SEQ", initialValue= 1, allocationSize = 1) private long id;
@ManyToOne(fetch=FetchType.LAZY) @LazyToOne(LazyToOneOption.NO_PROXY) @LazyGroup("EmployeeForTask") @JoinColumn(name="emp_name") private Employee employee; ... } {code}
Test code:
{code} entityManager.clear();
transaction.begin(); { Employee e = entityManager.createQuery("from Employee e", Employee.class).getSingleResult(); entityManager.remove(e); // NOTE: if deleted after the tasks, the scenario does not fail for (Task t : e.getTasks()) { entityManager.remove(t); } } transaction.commit(); {code} |
|