Introduction to session and persistence classes in the Hibernate framework of Java

The session object is used to obtain a physical connection to the database. The session object is lightweight, and an interaction is designed to be instantiated with the database every time. The persistent object is saved and retrieved through a session object.

Objects in a session should not remain open for a long time because they are usually not thread safe. They should be created and destroyed as needed. The main function of this conference is to provide instances of entity classes for creating, reading and deleting operation mappings. There may be one of the following three states in the instance at a given point in time:

Transient: a new instance of a persistent class that is not associated with a session and is not representative in the database and has no identification value is considered temporary by hibernate.

Persistence: you can do a transient instance persistence by associating it with a session. Persistent instances have a representation in the database and an identifier value associated with the session.

Independence: once we close hibernate session, the persistent instance will become a separate instance.

A session instance is serializable if its persistence class is serializable. A typical transaction should use the following statement:

If the session throws an exception, the transaction must be rolled back and the session must be discarded.

The whole concept of persistent class hibernate is to take the values from Java class attributes and persist them to database tables. A mapping file hibernate helps determine how to pull values from classes and map them to tables and related fields.

Java classes whose objects or instances are stored in database tables are called persistent classes in hibernate. Hibernate works best if these classes follow some simple rules, also known as the ordinary Java object (POJO) programming model. There are the following main rules for persistent classes, but these rules are not necessary.

A default constructor is required to persist all Java classes.

All classes should contain the IDs of hibernate and database in order to make it easy to identify the object. This property maps to the primary key column of the database table.

All properties will be persisted and should be declared private and have been defined in JavaBean style getxxx and setXXX methods.

The key function of hibernate, proxy, depends on the implementation of the interfaces of persistent classes or non final or declared public methods.

All classes do not extend or implement the EJB framework, which needs some special classes and interfaces.

POJO name is used to emphasize that a given object is an ordinary Java object, not a special object, better not an enterprise JavaBean.

A simple POJO example: Based on the above rules, we can define a POJO class as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>