The previous article (Portal: Hibernate learning note _01) introduced a brief introduction to what Hibernate is, how to build it, a detailed description of the configuration file, and a few basic API details of hibernate. Then this article will take a look at 5 aspects of Hibernate learning experience:
Entity rules in 1.hibernate
Object state in 2.hibernate
3.hibernate Advanced--First level cache
Transactions in 4.hibernate
Bulk queries in 5.hibernate (overview)
Entity rules in Ⅰ.hibernate
In hibernate use, you need to create the entity that corresponds to the database table and configure it in the mapping file. There are a few details to be aware of when creating entities.
(1) There are 5 things to note when creating an entity class:
1. Persistence classes provide parameterless constructs
2. The member variable is private and provides access to the common Get/set method. Properties to be provided
3. Properties in a persisted class should use the wrapper type as much as possible
4. The persistence class needs to provide an OID. Corresponds to the primary key column in the database
5. Do not use the final modifier class (Hibernate uses the Cglib proxy to generate proxy objects.) The proxy object is inherited by the proxy object. If it is final decorated. The agent cannot be generated.
(2) Primary key type
Natural primary KEY (uncommon): The column can be used as a primary key in the Business column of a table when there is a business column that is compliant, must have, and does not duplicate the feature.
Surrogate primary KEY (common): When there is no business column in the table, there must be a feature that does not have duplicate characteristics, and creates a column with no business meaning as the primary key.
(3) Primary key generation policy
Proxy primary Key
Identity: Primary key self-increment. The primary key value is maintained by the database. You do not need to specify a primary key when entering.
The primary key generation policy in sequence:oracle.
Increment: primary key self-increment. It is maintained by hibernate. The maximum ID value in the table is queried before each insert. +1 as the new primary key value.
Hilo: High-low-level algorithm. The primary key is self-increment. Maintained by hibernate. Not used during development.
Native:hilo+sequence+identity automatic three-choice one strategy.
UUID: Generates a random string as the primary key. The primary key type must be of type string.
In the actual development process can be based on the need to choose the appropriate primary key generation strategy, generally speaking than the common is to use native, usually in the entity Xxx.hbn.xml <generator class= "native" ></generator Configuration in > Properties
Natural PRIMARY Key
Assigned: Natural primary key generation strategy. Hibernate does not manage primary key values. Input by developers themselves.
The state of an object in Ⅱ.hibernate
In hibernate, objects are usually divided into three states:
1. Instantaneous status
No ID, no in session cache
1 Public voidfun1 () {2 //1. Get Session3Session session =hibernateutils.opensession ();4 //2. Control Transactions5Transaction tx =session.begintransaction ();6 //3. Perform the action7Customer C =NewCustomer ();//no ID, no transient state in session cache ==>8 9C.setcust_name ("HP");//instantaneous StateTen OneSession.save (c);//persistent state, with ID, in session cache A //4. Commit a transaction - tx.commit (); - //5. Close Resources theSession.close ();//Free | off-tube status, with ID, not in session cache -}
2. Persistence status
With ID, in the session cache
Persistence State Features: Any changes to the persisted state object will be synchronized to the database.
3. Free | Managed status
There is an ID, not in the session cache
During the development operation, these three states can be transformed into one another.
Ⅲ.hibernate Advanced--First level cache
Usually, the cache is for efficiency, in the most common computer, the CPU also exists cache, the hard disk also exists cache, memory also exists cache, and the first-level cache in Hibernate is also designed to improve the efficiency of the operational database.
There are two ways in which caching improves efficiency in hibernate:
1. Improve query efficiency
2. Reduce the sending of unnecessary modification statements
Transactions in the Ⅳ.hibernate
As we all know, there are four major characteristics () of ACID in business:
Atomicity (atomicity), consistency (consistency), isolation (isolation), persistence (durability)
The problems caused by transaction concurrency are: 1. Dirty read, 2. Non-repeatable read, 3. Phantom | Virtual read.
There are four conditions for the isolation level of a transaction: (The following numbers represent the problems associated with concurrency above)
READ UNCOMMITTED-123
Read COMMITTED-23
Repeatable READ (MySQL default level)-3
Serialization-No problem
So how do you specify the isolation level of the database in hibernate? Simply add the following line configuration to the Hibernate.cfg.xml file
<!-- Specify the isolation level when hibernate operates the database # # Specify a JDBC isolation Levels #hibernate. connection.isolation 1|2|4|8 0001 1 READ UNCOMMITTED 0010 2 Read submitted 0100 4 Repeatable Read + 8 serialization -- < name= "hibernate.connection.isolation">4</ Property>
So how do we manage matters in the project?
The transaction is opened before the business starts, and the transaction is committed after the business executes. An exception occurred during execution. rolls back the transaction.
The session object is required to manipulate the database at the DAO layer. The Service control transaction is also done using the session object. We want to make sure that the DAO Layer and service layer use the same session object
In hibernate, make sure to use the same session problem, Hibernate has helped us to solve. We developers only need to call the Sf.getcurrentsession () method to get the session object bound to the current thread
Note 1: Calling the Getcurrentsession method must match a section of the configuration in the master configuration
<!---<name= "Hibernate.current_session_context _class ">thread</Property>
NOTE 2: The session object is obtained by the Getcurrentsession method. When a transaction commits, the session is automatically closed. Do not call close manually.
On the service layer
DAO layer
Hibernate Learning Note _02