Hibernate Note 2, hibernate note

Source: Internet
Author: User

Hibernate Note 2, hibernate note

1. Persistence
1. Persistence ID OID
The database is called a primary key, and the ID attribute of the corresponding object is OID. Hibernate uses OID to identify whether two objects are the same object. OID generation is generally automatically processed by the program;
2. Persistence class
Concept: persistence is an entity class that can operate databases;
Persistence class = Object Class + ing file (Object Class Name. hbm. xml)
3. Write rules:
(1). classes must be modified using public;
(2). Implement the serialization interface; (public class User implements Serializable {....})
(3) the class cannot be modified using final; otherwise, the proxy object cannot be constructed;
(4). Attributes must be modified using private, and public-modified getter/setter methods are provided;
(5). You need to add the OID attribute;
(6). modify attributes using the packaging class (Long, Double, Integer, Character ...)
(7). public modification is required for construction without parameters, so that objects can be created through reflection;
4. Three statuses:
Key points: (1) whether the OID exists; (2) whether it is managed by the session;
I. Instantaneous State: No ID, no session;
Ii. Persistent state: There is an ID and a session. objects in this State automatically update the database when their attributes change, instead of calling the update method;
Iii. Free State (unmanaged State): with ID and no session;

Ii. Primary Key (OID) Generation Policy
1. Category:
Natural primary key: The field with business meaning is used as the primary key. The disadvantage is that the field may be repeated in the actual business. Once repeated, the model must be modified to redefine the primary key, which makes it more difficult to maintain the database;
Proxy primary key: The field that does not have business meaning is used as the primary key. Generally, it is named "ID" and generally an integer type, saving database space. The primary key generation policy is only valid for the proxy primary key !!
2. Generation policies: 6
Increment: auto-increment. Only the numeric type is supported (not recommended). The principle is to first query the maximum ID value in the database table, and then add 1. if a user has used this ID, then the registered user can use it again, leading to data duplication issues;
Identity: Automatic growth. Only data types are supported. The principle is to call the database growth mode. before using this policy, determine that the database in use supports automatic growth, such as mysql;
Sequence: The principle is to generate a sequence identifier by calling the database, provided that the database supports the sequence, such as oracle;
Native: It is most commonly used to select identity or sequence based on the database's ability to automatically generate tokens;
Uuid: uses the 128-bit UUID algorithm to generate the identifier, and is then encoded as a 32-bit hexadecimal string. Because the string occupies a large database space, it is not popular;
Assigned: manually assigned (specified by java program). This policy is used by default if the generator attribute of the id element is not configured in the ing relationship file;
3. Configure the generation policy: In the ing file (object class. hbm. xml) under the entity class package

1 <class name = "entity class full qualified name" table = "database table name"> 2 <id name = "OID name" column = "database table field name"> 3 <generator class = "native"> </generator> 4 </id> 5 ............... 6 </class>

Iii. Cache Mechanism
1. cache:
A temporary area in the memory is used to improve access efficiency. The hibernate cache mechanism is mainly used to improve query efficiency;
2. Level 1 cache:
(1) Definition: the session-level cache (consistent with the session lifecycle), which comes with a program and cannot be detached and is composed of a series of java sets (Map;
(2) Data Access time:
I. When the save/update method is called, it is saved to the database and then cached;
Ii. When you call the get method, first search from the cache, and then search from the database if there is no such thing, and then put it into the cache;
(3) verify the existence of a level-1 cache: the same object is searched twice. The first query will send an SQL statement, and the second query will not be sent;
(4) Internal Structure:
The cache zone and snapshot zone exist. The snapshot zone stores copies of the data in the cache zone, which cannot be modified by the programmer. When performing CRUD operations, the programmer actually operates the cache zone and commits transactions, hibernate compares the data consistency between the two regions. If the data is inconsistent, modify the database by referring to the cache data and save it to the snapshot area.
3. Secondary cache:
SessionFactory-level cache; plug-in form, you need to configure and import jar packages, has been replaced by redis;

Iv. Transaction Management
1. concept:
A logical set of minimum operation units is characterized by simultaneous success or failure to ensure operation integrity and security;
2. features:
Atomicity: The smallest operating unit, which is inseparable;
Consistency: the data before and after the operation is consistent;
Isolation: transactions between multiple threads do not affect each other;
Persistence: after the transaction is committed, it is stored in the database and cannot be changed;
3. problems that may occur without considering isolation
Dirty read/non-repeated read/virtual read
4. Solve the above problem: Set the isolation level
Read uncommited: do not solve any problem 1
Read commited: Solve the dirty read problem 2 default oracle isolation level
Repeatable read: solves the problem of dirty read and non-repeated read. 4. Default isolation level of mysql
Serializable: can be solved, but it becomes a single thread, low efficiency 8
5. Set the isolation level for hibernate
In the core configuration file (hibernate. cfg. xml), add the following statement:

  1         

6. Transaction Control at the service layer
(1) In the core configuration file (hibernate. cfg. xml), add the following statement:
  1         

(2) Use the getCurrentSession method of factory to obtain the session object
(3) when using the session object bound to the current thread, do not close it manually.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.