I have a Transactional method, inside it an entity is instantiated and inserted in the Hibernate context using persist method. Then some properties of the entity are changed (so it will be reflected in the database). What will happen if detach method is called on the entity and then some properties of the entity are changed. When the method finishes (and transaction commits), will Hibernate insert the entity and update the attributes to the point before the detach call?
For example:
@Transactional
public void transactionalMethod(){
MyEntity entity = new MyEntity();
getEntityManager().persist(entity);
entity.setAttribute1(data1);
entity.setAttribute2(data2);
getEntityManager().detach(entity);
entity.setAttribute3(data3);
}
Well, I'm answearing my own question for people having a similar question.
When using Spring Transactional method, all the inserts will be commited at the end of the transaction. If you use detach on an entity, all the updates (before or after detach) will be discarted, so no commits of updates will be done when transaction finishes.
If you want to reattach an entity to Persistence Context, you can do a merge or update method on the entity, then all changes to the entity will be commited when transaction finishes.
Hope this helps.