'Retrieve new entity (not proxied) inside a transaction in java
Retrieve NEW (not proxied) entity inside a transaction in Java with Hibernate
In java, it's possible to retrieve a new instance of a specific entity without retrieving the ones that hibernate proxies ?
Suppose that we have a method like this
@Transaction
public void parentMethod(String id){
childMethod(id); //perform operation on entity associated with id
repository.findById(id);//this needs to retrieve entity from new session
}
And the child nested method like this:
public void childMethod(String id){
//retrieves and perform save operation on entity associated with the id
EntityTest test=repository.findById(id);
.........update operations..............
repository.save(test);
}
Consider that i NEED to perform the findById, at the end of the parentMethod, to order the collections inside the entity (annotated with @OrderBy)
If i'm not wrong, when childMethod perform (for example) save operation on the entity, also proxies the result in the session, and if we (in the parentMethod) perform the findById it retrieves that entity and not a new one, so:
- childMethod retrieve entity with findById (nested collections are ordered)--> proxies result
- childMethod update entity with save (nested collections are not ordered)--> update the proxy with the result
- parentMethod perform a findById (nested collections are not ordered)--> retireves the proxy entity
How can i retrieve a new instance outside the session to ensure the collections order? considering that we are inside a transaction and if something goes wrong with the last findById i need the save operations to be rollback?
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
