Hibernate Framework-07-01-Operating Persistent Objects


Ways to reclaim memory

Null references will be reclaimed memory

In Java, null references will be recycled by Java's garbage collection mechanism.

Insert picture description here

The object will allocate space when it is instantiated, that is, space will be allocated only in the third row. Assign the first address of the memory heap area to C1

In line 4, the address is also assigned to C2;

A new object will be created on the 5th line, and the new address value will be assigned to C2;

Isolated references will be reclaimed memory

Insert picture description here
There is an attribute of the order collection type in the User object.

Cache

Insert picture description here
The role of the cache is to reduce the pressure on the permanent storage source (the value database here)

Session cache

Insert picture description here
The internal implementation is always referenced through the internal interface.
Let a reference of a collection type always exist.
The essence is to put the object in the collection

SessionFactory.openSession();初始化session对象,初始化Session。

Insert picture description here

When the Session is inquired, look at the cache first, and then look at the database
when there is no. When calling get and save methods, they are all in the cache

Insert picture description here

Insert picture description here
Clean up! =
Clean up Clean up = Make cached data consistent with database data
Update the database synchronously according to the Session cache

Session.flush();清理缓存,会被roulBack回滚事务
Session.close();会永久提交,不会被回滚

The Load and get methods do not clean up the cache, and the get and load methods read the cache.
The cache will be judged first, so the database will not be read, and the dirty data (error data) of the database will not be read

Session snapshot

Insert picture description here
The save method will put the data in the cache, and create a backup (snapshot) in the
cache. When cleaning the cache, it will judge whether the data in the cache is consistent with the snapshot. If it is inconsistent, it will update the new data to the database.

Insert picture description here
Menual: Manually clean up the cache, the commit method is not performing cleanup

Insert picture description here

COMMIT: It is suitable for more queries, and the data is not safe

Session cache function

Insert picture description here

Stack overflow exception

Insert picture description here

Insert picture description here

Will not create a new object, but the OID assignment;

The life cycle of Hibernate objects

The life cycle of an entity object

Insert picture description here
Temporary-"Persistence-"Free-"Delete

Insert picture description here
Temporary object specific: the cache has records, the data table has no corresponding records
Persistence features: the database has records, the cache also has corresponding
features Free object features: the database may have records, but the cache does not correspond to the
deleted object Features: not in the cache, and plan to delete the database recording.

Insert picture description here
The close method will clear the cache object and modify the cache flag (identifying whether the Session is available) The
clean method only clears the cache

In addition to the persistent state will not be recycled, other states may be recycled, because the persistent state must have references.

Hibernate operating persistent objects

Session的Save()

Insert picture description here

Session的update()

Insert picture description here

The label of the fourth point can be used to realize that the database is not updated when it is consistent with the snapshot,
but there will be an additional operation to query the database, so it is generally not used.

Insert picture description here

Session的saveOrUpdate()

Insert picture description here

Insert picture description here
By default, the attribute is a temporary object as long as OID=null. The
premise is the referenced object

The delete method of Session

Insert picture description here
Insert picture description here

Session的load() get()

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/115205958