In Hibernate, when an object's fields are lazy loaded, can I only change one of the fields and do update(object)?
Considering this:
@Entity
class Order
{
@Id
public int id;
@OneToOne
Person person;
@OneToOne
Address address;
//Other Fields
}
I have an Order which is lazy-loaded. I change the Address for this Order. Note that I have not accessed the Person (so it is not loaded ). Then I do
update(order);
I would like to know what happens in this case to database records in person table. I don't want the associated person record to be updated with nulls. And when I tested,开发者_JAVA百科 it was not updated with nulls.
From my understanding, the person reference is assigned a proxy object and not a null.
Does Hibernate take care that if the object was not loaded (in this case person object), no
associated record or records are changed (in this case, person table record ) ?Is it safe to do such an update ?
Thanks !
JPA only updates entities that are changed. I’m not aware of any update() method, you can just use commit() or flush() and all entities in you session that are changed will be persisted to the database.
Lazy loaded properties are just proxy objects that are not loaded at all, you can also do this manually by invoking: [entityManager.getReference][3]
[3]: http://download.oracle.com/javaee/5/api/javax/persistence/EntityManager.html#getReference(java.lang.Class, java.lang.Object)
精彩评论