Manytomany Hibernate - study

Asked

Viewed 92 times

0

I am studying the operation of Ibernate and I have some doubts....

Folks anyone can tell me how Hibernate works on the issue of organizing this information Manytomany?

on this example site: https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/

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

My question is how does it work for Hibernate to 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 be for change? how/how much it will understand when it will be creation and exclusion?

1 answer

1

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çõesSQL 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.

inserir a descrição da imagem aqui

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:

  • the application loads an object in the first session

  • the object is passed to the UI layer

  • some modifications are made to the object
  • the object is returned to the business logic layer
  • the application persists these modifications by calling update() in a
    second session.

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

  • 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.

  • 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?

  • Because it is managed the state of your object. Any change in the state of your object. Look life cycle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.