Hi,
When I refresh entities, hibernate by default reads in any M:1 or 1:1 relationships that the entity points to or that point to the entity.
Note: I am not using cascade persist, this is just the default behaviour of hibernate.
I have a 1:1 between Person and Address and both are persistent.
I set a variable on Person and then re-read Address, which will also re-read the Person.
I can see this in SQL generated.
Because person is re-read I would have thought the change in memory would have been overriden but it is not.
In code it is:
Code:
address.setPerson(person);
person.setAddress(person);
person.setFirstName("tony");
entityManager.persist(address);
entityManager.persist(person);
entityManager.getTransaction().begin();
entityManager.getTransaction().commit();
person.setFirstName("adam");
entityManager.refresh(address);
System.out.println("Person name is now..." + address.getPerson().getFirstName());
This outputs:
Person name is now... adam
I would have thought it would have been tony, comments please...
Is there ever a case when something in memory will be overriden?