Making the objects persistent
The newly instantiated instances of a persistent class are considered transient by Hibernate
. We can transform a transient instance into persistent by associating it to a session:
DomesticCat fritz = new DomesticCat();
fritz.setColor(Color.GINGER);
fritz.setSex('M');
fritz.setName("Fritz");
Long generatedId = (Long) sess.save(fritz);
Condition of objects 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 has no 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 object persistent (e deixe o Hibernate gerenciar as instruções
SQL that will be needed to execute this transition).
Persistent - a persistent instance has a database representation and an identifier. It may have been saved or loaded, so it is within the scope of a Session. Hibernate will detect any change made to a persistent object and synchronize its state with the database when it completes the working drive. Developers do not execute manual instructions for UPDATE
, or instructions from DELETE
when the object becomes transient.
Detached – a disconnected 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 disconnected instance can be coupled to a new Session in the future, making it persistent again (and all modifications suffered). This feature enables a programming model for long-run work units, which requires a user wait time. We can call them application transactions, ie a unit of work from the user’s point of view.
Automatic state detection
The use and semantics of saveOrUpdate()
seem to be confusing for new users. At first, while you don’t try to use instances of one session in another session, you don’t need to use update(), saveOrUpdate()
, or merge()
. Some entire applications will never need to use these methods.
Generally, update()
or saveOrUpdate()
are used in the following scenarios:
saveOrUpdate()
does the following:
- if the object is already persistent in this session, do nothing
- if another object associated with the session has the same identifier,
make an exception
- if the object does not have an identifier property
salve-o
()
- if the object identifier has the value assigned to the object
recently instantiated,
salve-o()
- if the object is versioned by an or , and the value
of the version property is the same value assigned to the object
recently instantiated, save() the same
- otherwise update() the object
Erasing persistent objects
To Session.delete()
will remove an object state from the database. Of course your application may still retain a reference to a deleted object. It’s best to think about delete()
how to make a persistent instance become transient.
sess.delete(cat);
Operation of the Hibernate Compartment:
Cascade save-update
: This is how the name suggests, when you save or update the "Father" class, the "Daughters" classes are also saved or updated.
Cascade Delete
: Delete Cascade causes children to be deleted if the parent is deleted.
Cascade delete-orphan
: There is still the delete-orphan
, when you save or update the parent class, the child records that were marked as removed are in fact deleted.
Surely this is a very important and essential subject for project development using Hibernate. Take a look at these links below that from the documentation itself. It will help you understand a little more about ciclo de vida do Hibernate
.
https://docs.jboss.org/hibernate/orm/3.5/reference/pt-BR/html/objectstate.html#objectstate-loading
https://www.devmedia.com.br/cascade-hibernate-conhecendo-diferentes-tipos/28892
Thanks for the information, I need to know how Hibernate controls manytomany information, I need to know how he knows that an object is being altered and/or deleted, I need to know how he does it, I would appreciate it if you could contribute to that if you know, obg
– HimorriveL
If you read the post you will understand that it controls the cycle of your objects a look at the link and this what it does.
– LR10
In this link I see the commands performed for Hibernate to perform such functions Many to Many, but I did not find with Hibernate knows if it is to change or delete in case to insert: Stock stock = new Stock(); Stockdailyrecord stockDailyRecords = new Stockdailyrecord(); stockDailyRecords.setStock(stock); stock.getStockDailyRecords(). add(stockDailyRecords); Session.save(stock); Session.save(stockDailyRecords); now as it knows it must be inserted and or deleted?
– HimorriveL
Because it is managed the state of your object. Any change in the state of your object. Look life cycle.
– LR10