Hibernate Object State

I am testing Hibernate, here is the situation and the code:

public static void main(String[] args) { SessionFactory factory = HibernateUtil.getSessionFactory(); Tag tag; // (case A) Session session = factory.getCurrentSession(); Transaction tx = session.beginTransaction(); tag = (Tag) session.get(Tag.class, 1); tag.setName("A"); tx.commit(); // session is automatically closed since it is current session and I am committing the transaction // session.close(); //here the tag object should be detached //(case B) session = factory.getCurrentSession(); tx = session.beginTransaction(); // tag = (Tag) session.merge(tag); // I am not merging tag.setName("B"); //changing // session.update(tag); tx.commit(); // session.close(); } 

It does not update for case B ( tag.setName("B") does not work).

Then I uncomment session.update(tag); in case B , it now works. It should give an error due to the fact that the object does not merge with the transaction case B

We can say that we use factory.getCurrentSession() , so there is no need to combine it, but if you replace it with factory.openSession(); and close the session after each case, it still works (with the update invocation in case B ). So, in what sense do we call an object separate?

+4
source share
2 answers

case A: the session is not closed, and the tag object is in a constant state, and it (the tag object) is attached to the current session.

case B: here the session may be the same from the first transaction, you change the value of the tag object, which is in the Persistent state. Persistent state represents existence of object in permanent storage. There will be connection between the object in memory and in database through the identifier. Any change in either of these two will be reflected in other (when transaction is committed). Persistent state is dependent on session object. First, session has to be open (unclosed) Persistent state represents existence of object in permanent storage. There will be connection between the object in memory and in database through the identifier. Any change in either of these two will be reflected in other (when transaction is committed). Persistent state is dependent on session object. First, session has to be open (unclosed) [this is true in your case] , and second, the object has to be connected to the session. If either of these two is not true, then the object moves into either transient state or detached stage. , and second, the object has to be connected to the session. If either of these two is not true, then the object moves into either transient state or detached stage.

An object is in a disconnected state in the following case: Detached state arises when a persistent state object is not connected to a session object. No connection may be because the session itself is closed or the object is moved out of session. Detached state arises when a persistent state object is not connected to a session object. No connection may be because the session itself is closed or the object is moved out of session.

+3
source

About object states:

Hibernate identifies three object states: Persistent, Transient, and Detatched.

  • The transition state of an object is objects that have never been associated with a Hiberbate session. This is usually a new instance of a constant class that has no representation in the database and does not have an identifier value.

  • The persistent state of an object is the objects that are currently associated with a Hiberbate session and have a view in the database and have an identifier value.

  • A separate state of an object is objects that have moved from a constant state and have a representation in the database. When a session is closed, the state of the object changes from permanent to disconnected.

Example:

 ... // Tag in a transient state Tag tag = new Tag(); tag.setName("A"); // Tag in a persistent state Long id = (Long) session.save(tag); // Tag in a detached state session.close(); ... 
0
source

Source: https://habr.com/ru/post/1402381/


All Articles