Possible object status options

As you may have noticed, when an object is attached to Hibernate, its state is called Persistent or Managed . How right? Persist or Managed?

It is possible and so, and so. According to the JPA specification, an object has a Persist state, and according to the Hibernate specification, its state is called Managed.

In Hibernate, work with the database is carried out through an object of type org.hibernate.Session . According to JPA, the class should be called javax.persistence.EntityManager. This isn't really a big problem, since both types are interfaces.

The org.hibernate.Session interface is declared like this:

interface Session extends java.lang.AutoCloseable, javax.persistence.EntityManager, HibernateEntityManager,    QueryProducer, java.io.Serializable, SharedSessionContract {

}

Therefore, the Session interface has all the methods that are in the EntityManager interface . But it also has its own, which it inherited from earlier versions of Hibernate, when there was no JPA specification yet.

This whole situation is described as follows:

Possible object status options

Let's take a look at all the methods that the session interface has, as well as the nuances of their work.

The nuances of the persist() method

When saving an object to the database, you need to remember two things.

First, saving the object will eventually result in the execution of one of the SQL commands: INSERT or UPDATE. Secondly, these actions will not occur immediately after calling the object's methodsession, but only after closing the transaction.

Let's consider some simple situation, for example, you have a User class :

@Entity
public class User {
	@Id
	@GeneratedValue
    public Integer id;

	@Columnt(name=”user_name”)
    public String name;
}

Let's save its object to the database using the persist() method .

User user = new User();
user.setName("Kolyan");
session.persist(user);

The purpose of this method is to save a new object to the database. If there is no such object yet, then it will be added to the database by calling the INSERT SQL method .

If the object is already in the database, then nothing will happen. However, a third case is also possible - an attempt to save an object with the Detached status to the database. In this case, an exception will be thrown. Example:

User user = new User();
user.setName("Kolyan");
session.persist(user);

session.evict(user);     // detach the object from the session
session.persist(user); // a PersistenceException will be thrown here!

The nuances of the save() method

The save() method was inherited by the current Hibernate from its previous versions. At its core, it is very similar to the persist() method , it also adds a new record to the table using the INSERT method. However, it has several interesting nuances.

First, this method returns a value - the new ID of the object . As you already know, objects usually do not have an ID before being added to the database and it is already assigned by the database. So, the save() method of the session object returns the ID that was assigned to the saved object.

Important! According to the Hibernate ID specification, it can be any serializable object, not just a number. It can be a string, a number, an enum, anything in general that can be put entirely in one column of a table in the database.

The save() method has a Serialized result type, so its result must be cast to the correct type:

User user = new User();
user.setName("Kolyan");
Integer id = (Integer) session.save(user);

Also, the save() method has a different behavior in the case of the object's Detached state. It considers such an object as new and simply adds another entry:

User user = new User();
user.setName("Kolyan");
Integer id = (Integer) session.save(user);

session.evict(user); 	// detach the object from the session
Integer id2 = (Integer) session.save(user);

The id and id2 variables will be different. Two records will be added to the table in the database, one for each save() operation .

undefined
1
Task
Module 4. Working with databases, level 11, lesson 1
Locked
task1101
task1101