I have a problem with a transactional method that changes an entity and wants to update it in some other way.
At first i get the entity A from the database with the entitymanager method "get". Then i get a related entity B where A to B is type of one to one (optional). (So the id field of B is inside the table of A). Now i want to remove the entity B via a service method. Therefore i have to use the ID of B.
Inside the service method i get B from the entity manager (now B'). Then i get A' from the aquired B'. Then i remove the link A' to B' when it is present via A'.setB(null) followed by a serviceOfA.save(A').
Then i delete B' via serviceOfB.delete(B').
When the method for removal of B via the id is completed i want to change properties of the instance A. Create another instance of B for example. Now when i get A via the entitymanager again hibernate throws a org.hibernate.exception.ConstraintViolationException for an object that should be added to the new B'' instance that is added to A.
I think the issue has something to do with the removal method that changed the instance A' and therefore A can not be reloaded. But how can i reload the new state of A? Please have a look below:
@Transactional
public void compeleteSomething(
@NonNull String location,
@NonNull String nameOfA) throws SomeException{
A a= serviceOfA.get(nameOfA);
B b= a.getB();
someService.removeBAndLinkToA(b.getId()); // <-- maybe here is the error
B newInstanceOfB = someService.createBOn(location);
someService.setBonA(serviceOfA.get(nameOfA), newInstanceOfB); // <-- serviceOfA.get() throws error
[...]
}
And here the method of someService.removeBAndLinkToA(#)
@Transactional
public void removeBAndLinkToA(
@NonNull Long id) {
B b = serviceOfB.get(id);
A a = b.getA();
if (a!= null) {
a.setB(null);
serviceOfA.save(a); // <-- This should cause the error?
}
serviceOfB.delete(b);
}
How can i avoid this issue? Many thanks!