Get the App
SLTechnology News&Howtos  ›  Network Security  › 

How to operate Hibernate

Shulou Source: shulou.com Published: 2022-05-31 21:47:37 11月14日 Update

This article mainly explains "how to operate Hibernate". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to operate Hibernate!

First, use Hibernate API

Create a tool class management session factory

It should be noted here:

(1) SessionFactory does not need to be created repeatedly

(2) if you start a session with openSession (), you need to close it.

Second, use Hibernate to query, modify and delete operations.

1. Use Hibernate to query data

Load an instance of the specified type through the primary key value

2. Use Hibernate to modify and delete the department

Modify the department

Delete a department

3. Code demonstration:

3. Three states of Java objects in Hibernate

The transition between three states

Fourth, refresh cache mechanism

1. When caching (calling the flush () method of Session), Hiberante detects the object in the persistent state in Session to determine whether the object's data has changed

2. To refresh the cache is to synchronize the database with the Session cache.

3. A dirty check is performed when the cache is flushed

4.Session flushes the cache at the following points in time

Call the flush () method of Session

Call the commit () method of Transaction

The 5.commit () method flushes the cache first.

Fifth, summarize the use of Hiberante API methods

VI. Hiberante Association Mapping

1. The most common relationship between classes is association.

One-way association

Two-way association

2. Many-to-one relationship

Configure a many-to-one association from Emp to Dept

(1) Emp persistence class

(2) Emp.hbm.xml

3. One-to-many relationship

Configure one-to-many associations from Dept to Emp

(1) Dept persistence class

(2) Dept.hbm.xml

4. Many-to-many relationship

Configure bidirectional many-to-many associations for Project and Employee

(1) Project and Employee persistence classes

(2) Project.hbm.xml, Employee.hbm.xml

At this point, I believe you have a deeper understanding of "how to operate Hibernate", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Association cache method object data state department query configuration between content bidirectional that is one level learning practical deeper consistent code interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB Apple Linux Huawei