main difference is
get() and load() returns >>>>>>>>>> object
but find() returns >>>>>>>>> List
Tuesday, February 2, 2010
cascade Attribute in Hibernate .hbm.xml
The cascade="all" attribute is a very important one. It explains how Hibernate should act while manipulating connected objects. In our specific situation, when an Order is created, we definitely want all of its OrderItems to be created as well, and, of course, when an Order is deleted, we also want all of its OrderItems to be deleted. There are three more options cascade attribute can hold, none, save-update, and delete
1) cascade="none", the default, tells Hibernate to ignore the association.
2) cascade="save-update" tells Hibernate to navigate the association when the
transaction is committed and when an object is passed to save() or
update() and save newly instantiated transient instances and persist changes to
detached instances.
3) cascade="delete" tells Hibernate to navigate the association and delete persistent
instances when an object is passed to delete().
4) cascade="all" means to cascade both save-update and delete, as well as
calls to evict and lock.
5) cascade="all-delete-orphan" means the same as cascade="all" but, in addition,
Hibernate deletes any persistent entity instance that has been removed
(dereferenced) from the association (for example, from a collection).
6) cascade="delete-orphan" Hibernate will delete any persistent entity
instance that has been removed (dereferenced) from the association (for
example, from a collection).
1) cascade="none", the default, tells Hibernate to ignore the association.
2) cascade="save-update" tells Hibernate to navigate the association when the
transaction is committed and when an object is passed to save() or
update() and save newly instantiated transient instances and persist changes to
detached instances.
3) cascade="delete" tells Hibernate to navigate the association and delete persistent
instances when an object is passed to delete().
4) cascade="all" means to cascade both save-update and delete, as well as
calls to evict and lock.
5) cascade="all-delete-orphan" means the same as cascade="all" but, in addition,
Hibernate deletes any persistent entity instance that has been removed
(dereferenced) from the association (for example, from a collection).
6) cascade="delete-orphan" Hibernate will delete any persistent entity
instance that has been removed (dereferenced) from the association (for
example, from a collection).
Advantage of Hibernate session load() vs get()
If load() can’t find the object in the cache or database, an exception is
thrown.
The load() method never returns null.
The get() method returns
null if the object can’t be found.
The get() method will return a FULL initialized object if nothing is on the session cache, that means several DB hits depending on your mappings.
While the load() method will return a proxy (or the instance if already initialized), allowing lazy initialization and thus better performance
Choosing between get() and load() is easy: If you’re certain the persistent
object exists, and nonexistence would be considered exceptional, load() is a
good option. If you aren’t certain there is a persistent instance with the given
identifier, use get() and test the return value to see if it’s null.
thrown.
The load() method never returns null.
The get() method returns
null if the object can’t be found.
The get() method will return a FULL initialized object if nothing is on the session cache, that means several DB hits depending on your mappings.
While the load() method will return a proxy (or the instance if already initialized), allowing lazy initialization and thus better performance
Choosing between get() and load() is easy: If you’re certain the persistent
object exists, and nonexistence would be considered exceptional, load() is a
good option. If you aren’t certain there is a persistent instance with the given
identifier, use get() and test the return value to see if it’s null.
Subscribe to:
Posts (Atom)