0

I'm using Hibernate with JTA (container managed transactions).

Let's say I have the following model: Employee with many-to-one relations to Office and Team. I've set up bidirectional associations.

class Employee{
   Office office;
   Team team;
}

class Office{
   List<Employee> employees;
}

class Team{
   List<Employee> employees;
}

Now the data:

Emp 1 with Ofc 1 and Team 1

Emp 2 with Ofc 1 and Team 2

The problem: I iterate over the Team 1 employees (i.e. just emp 1) and delete them. Then within the same transaction I execute HQL select to get Ofc 1. Now within Ofc 1 employees list I expect to find just emp 2 (since emp 1 has been deleted on the previous step) but the result is that both employees are still there.

I tried flushing the entity manager after the deletion: same thing.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sierre
  • 93
  • 1
  • 4
  • I had a same kind of error but in my case flushing the *session* after the delete did the trick – jpprade Oct 22 '14 at 13:23
  • I tried the following: em.unwrap(org.hibernate.Session.class).flush(); No success. Thanks for commenting. – sierre Oct 22 '14 at 13:59

1 Answers1

0

As required by JPA standard, Hibernate will unschedule removal if PERSIST operation is applied/cascaded to the removed entity.

Make sure to remove all references to the deleted entity from all the other managed entity instances.

Community
  • 1
  • 1
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110