Hibernate ing file XXX. HBM. xml configuration ing element details-object identifier (OID)

Source: Internet
Author: User

 

Object Identifier
In relational database tables, the primary key is used to identify records and ensure the uniqueness of each record. In Java, you can determine whether the two objects are equal by comparing whether the memory addresses of the objects referenced by the two variables are the same, or comparing whether the values of the objects referenced by the two variables are the same. To solve the differences between the two, Hibernate uses an object identifier (OID) to identify the uniqueness of an object. Oid is the equivalent of a primary key in a relational database in a Java object model. At runtime, Hibernate maintains the correspondence between Java objects and records in database tables based on the oId. The following code calls
Load () method to load the user objects whose oid is 1 or 3 respectively.

Transaction Tx = session. begintransaction ();
User user1 = (User) Session. Load (user. Class, new long (1 ));
User user2 = (User) Session. Load (user. Class, new long (1 ));
User user3 = (User) Session. Load (user. Class, new long (3 ));
System. Out. println (user1 = user2 );
System. Out. println (user1 = user3 );
When the application executes the preceding code, it loads the user object whose oid is 1 for the first time, searches for the record whose ID is 1 from the database, and then creates the corresponding user instance, save it in the session cache, and assign the instance reference value to the variable user1. When loading an object whose oid is 1 for the second time, the reference value of the instance whose oid is 1 in the session cache is directly assigned.
To the variable user2. Therefore, the result of expression user1 = user2 is true.

 

Generator provides a unique identifier for each pojo instance. Generally, we use "native ". Class indicates an instance implemented using the Generator interface net. SF. hibernate. Id. identifiergenerator, including:
You can use different policies to generate identifiers. Table 1 is the built-in identity generation policy of hibernate.
Table 1 hibernate identity Generation Policy

Identifier Generator

Description

Increment

Applies to the proxy primary key. It is automatically generated by hibernate in incremental mode.

The primary key increments in numerical order. The implementation mechanism of this method is to maintain a variable in the current application instance to save the current maximum value, and then add 1 as the primary key each time the primary key needs to be generated. This method may cause problems: it cannot be used in a cluster.

Identity

Applies to the proxy primary key. The identifier generated by the underlying database.

Use the primary key generation mechanism provided by the database. Such as the primary key generation mechanism in DB2, SQL Server, and MySQL.

Sequence

Applies to the proxy primary key. Hibernate generates identifiers Based on the sequence of the underlying database, which requires that the underlying database supports sequences.

Use the sequence mechanism provided by the database to generate the primary key. For example, sequence in oralce.

HiLo

Applies to the proxy primary key. The identifier generated by the high/low algorithm of the hibernate branch. The primary key generation mechanism implemented by the HI/lo algorithm requires additional database tables or fields to provide a high value source.

Seqhilo

Applies to the proxy primary key. Use a high/low level algorithm to efficiently generate long, short, or Int type identifiers.

Similar to hilo, the primary key generation mechanism implemented by the HI/LO algorithm requires sequence in the database, which is suitable for databases that support sequence, such as oracle.

Native

Applies to the proxy primary key. Automatically Select Identity, sequence, or HiLo based on the way the underlying database automatically generates identifiers.

Hibernate uses identity, Hilo, and sequence as the primary key generation method based on the database used.

UUID. HEX

Applies to the proxy primary key. Hibernate uses the 128-bit UUID algorithm to generate a hexadecimal value (represented by a 32-Bit String After encoding) as the primary key identifier.


UUID. String

Applies to the proxy primary key. UUID is encoded into a 16-character string.

Similar to UUID. HEX, the generated primary key is not encoded (Length: 16) and cannot be used in the PostgreSQL database.

Assigned

Applicable to natural primary keys. The Java external application is responsible for generating identifiers and specifying one before saving.

Foreign

Applies to the proxy primary key. Use the identifier of another associated object.

The following example:
1. Specify parameters:
<ID name = "ID" unsaved-value = "0">
<Generator class = "sequence">
<Param name = "sequence"> seq_child </param>
</Generator>
</ID>
Sequence is used and is suitable for Oracle databases;

 

2. Use the following methods in the configuration file to add fields to the database in SQL Server2000:
<ID name = "ID" type = "long" unsaved-value = "0">
<Column name = "ID" SQL-type = "numeric" not-null = "true"/>
<Generator class = "Identity"/>
</ID>
Here is mainly: Identity: indicates that the SQL Server2000 Database provides the sub-increment field by itself. If you want hibernate to provide it by yourself, use the increment keyword to implement it.

 

3. If the primary key in the table is of the string type: You can use the method provided by hibernate to realize the unique primary key:
<ID name = "ID" type = "string" unsaved-value = "null">
<Column name = "CID" SQL-type = "char (32)" not-null = "true"/>
<Generator class = "UUID. Hex"/>
</ID>
UUID. HEX: uses the 128-bit algorithm to generate a 32-Bit String. The most common method. Applicable to all databases.

 

Important knowledge points: (hibernate ing Rules)
1. If there is a department table and an employee table, and the employee table has a dep_id, the relationship between the department class and the employee class is one-to-one:
Available: (use the following in department-class Departments)
Department class:

Private set staffs = new treeset ();

XML file:
<Set name = "staffs">
<Key column = "dep_id"/>
<One-to-learn class = "HBP. SYS. Data. Staff"/>
</Set>
If it is a list, you need to use the index <index> </index>. The specific meaning is not very clear.

2. If a department must have a principal, that is, the Department table (tb_department) has a field: staff_id.
The relationship between the Department and the owner is similar-to-one.
Department class:

Private staff;

XML file
<Role-to-one name = "staff" class = "HBP. SYS. Data. Staff" column = "staff_id"/>

3. many-to-many relationships. Generally, we create an intermediate Association table. I used roles and permissions as an example,
Right (ID, name) role (ID, name) Intermediate table: tb_role_right (role_id, right_id)
The right class has a role set: private set roles = new treeset ();
The role class also has a set of right: private set rights = new treeset ();
The two are obviously many-to-many relationships. They are implemented using transform-to-transform;
XML file
Right. HBM. xml:
<Set name = "Roles" table = "tb_role_right" cascade = "all">
<Key column = "right_id"/>
<Role-to-role column = "role_id" class = "HBP. SYS. Data. role"/>
</Set>
The role. HBM. xml file is similar to the following:
<Set name = "rights" table = "tb_role_right" cascade = "all">
<Key column = "role_id"/>
<Export-to-export column = "right_id" class = "HBP. SYS. Data. Right"/>
</Set>

 

4. Several noteworthy issues:
A) in XML? In the ing file, the full name of the class must be used for writing the Class Name: package + class name, for example: (HBP. SYS. data. staff), this error made me spend half a day. :(
B) I wrote
Session. Delete ("from right as right where right. ID =" + id );
The Program reported an error. After a long time, I tracked it to the end and suddenly realized that when hibernate resolved the SQL statement
Right is used as the right connection ("reserved word") in the database. Alas, this keyword cannot be used at will ,:)

 

5. For hql queries in hibernate, different object types are returned based on your SQL statements.
If you use session. Find (string hql)
Generally, a list is returned, such as: from staff; a set containing all employee objects is returned.
If your hql is: Select count (*) from staff; then an integer object is returned.
If the hql you are using is: Select count (distinct staff. Name), count (*) from staff; then an object is returned.
That is, object []. You need to convert it to object [] first, and then obtain [0], [1].
I don't know how hibernate handles this design. It feels good or bad. The good thing is that you can use a find to obtain any query.
It is difficult to process the returned results based on hql, which is prone to errors.

Source:

Http://blog.sina.com.cn/s/blog_0e598e1d0100eeyo.html

Http://hanshuo528.bokee.com/viewdiary.23028288.html

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.