Making persistent objects
Recently instantiated instances of a persistent class are considered transient by Hibernate
. We can transform a transient instance into persistent by associating it with a session:
DomesticCat fritz = new DomesticCat();
fritz.setColor(Color.GINGER);
fritz.setSex('M');
fritz.setName("Fritz");
Long generatedId = (Long) sess.save(fritz);
Object state in Hibernate
Hibernate defines and supports the following object states:
Transient - An object is transient if it was instantiated using only the new operator and was not associated with a Hibernate Session. It does not have a persistent representation in the database and has not been assigned any identifier. Transient instances will be destroyed by the garbage collector if the application does not keep its reference. Use a Hibernate Session to make the persistent object ( e deixe o Hibernate gerenciar as instruções
SQL that will be required to perform this transition).
Persistent - A persistent instance has a representation in the database and an identifier. It may have been saved or loaded, so it is in the scope of a Session. Hibernate will detect any changes made to a persistent object and synchronize its state with the database when you complete the unit of work. Developers do not execute manual statements of UPDATE
, or instructions of DELETE
when the object becomes transient.
Detached - A detached instance is an object that has been persisted but its Session has been closed. The reference to the object remains valid, of course, and the detached instance can be bound to a new Session in the future, making it persistent again (and all modifications undergone). This feature enables a scheduling model for long-running work units, which requires a user timeout. We can call them application transactions, that is, a unit of work from the user's point of view.
AutomaticStatusDetection
TheuseandsemanticsofsaveOrUpdate()
appeartobeconfusingtonewusers.Atfirst,aslongasyoudonottrytouseinstancesofasessioninanothernewsession,youdonotneedtouseupdate(),saveOrUpdate()
,ormerge()
.Someentireapplicationswillneverneedtousethesemethods.
Generally,update()
orsaveOrUpdate()
areusedinthefollowingscenarios:
saveOrUpdate()
doesthefollowing:
- Iftheobjectisalreadypersistentinthissession,donothing
- Ifanotherobjectassociatedwiththesessionhasthesameidentifier,throwanexception
- Iftheobjectdoesnothaveanidentifierproperty
salve-o
() - iftheidentifieroftheobjecthasthevalueassignedtotheobjectrecentlyinstantiated,
salve-o()
- iftheobjectisversionedbyoneor,andthevalueoftheversionpropertyisthesamevalueassignedtotheobjectrecentlyinstantiated,save()thesame
- otherwiseupdate()theobject
DeletingPersistentObjects
%wcwillremoveanobjectstatefromthedatabase.Ofcourseyourapplicationmaystillretainareferencetoanerasedobject.ItisbesttothinkofSession.delete()
howtomakeapersistentinstancebecometransient.
sess.delete(cat);
HowHibernatecascadeworks:
delete()
:Asthenamesuggests,whenasaveoranupdateismadeinthe"Parent" class, the "Daughters" classes are also saved or updated.
Cascade save-update
: Cascade delete causes children to be deleted if the parent is deleted.
Cascade Delete
: There is still Cascade delete-orphan
, when you save or update the parent class, child records that were marked as removed are in fact deleted.
This is certainly a very important and essential subject for developing projects using Hibernate. Take a look at this links below that of the documentation itself. It will help you understand a little more about delete-orphan
.
link
link