ManyToMany Hibernate - study

0

I am studying hibernate and I am having some doubts ....

in this example site: link

we have the post table with the ID and TITLE fields, we have the table tag also with the ID and TITLE fields and finally the ManyToMany table that links this information through the post_tag table with the post_id and tag_id columns.

My question is how does hibernate handle the changes? let's assume that today in the post_tag table I have post_id = 1 and tag_id = 1 and I need to change to post_id = 1 and tag_id = 2. How does this change issue work? how does hibernate understand that it will change? how will he understand when it will be creation and deletion?

    
asked by anonymous 30.01.2018 / 14:47

1 answer

1

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:

  • Theapplicationloadsanobjectinthefirstsession

  • TheobjectispassedtotheUIlayer

  • Somemodificationsaremadetotheobject
  • theobjectisreturnedtothelogicalbusinesslayer
  • Theapplicationpersiststhesemodifications,callingupdate()ina
    secondsession.

saveOrUpdate()doesthefollowing:

  • Iftheobjectisalreadypersistentinthissession,donothing
  • Ifanotherobjectassociatedwiththesessionhasthesameidentifier,throwanexception
  • Iftheobjectdoesnothaveanidentifierpropertysalve-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

    
30.01.2018 / 16:52