Hibernate ing Learning (1)

Source: Internet
Author: User

Hibernate ing Learning (1)

Today, this blog will mainly learn about how to map object classes to tables in hibernate. First, let's take a look at "common primary key generation policies"

Common primary key generation policies of hibernate

In hibernate, each primary key must define a corresponding primary key generation policy, which is used to generate a unique identifier for persistence class instances.
1. assigned
In hibernate, if you do not want to use the primary key generation policy of hibernate, You need to specify the primary key. In this case, you need to use assigned as the primary key generation policy.
When using assigned, you must manually specify the id. For example, you can run the following code twice in a row:

transaction = session.beginTransaction();UserInfo info = new UserInfo();info.setUserName("aa");info.setUserPass("vvv");session.save(info);transaction.commit();

During the first execution, the primary key id is 0 by default, and the second execution will still use the default value because we do not specify the id ourselves. Therefore, hibernate will throw the following exception:

2. increment
Here, we use the hibernate primary key auto-increment generation policy, mainly for long, short or int type to generate a unique identifier. It can be used only when no other process inserts data to the same table, do not use it in a cluster and do not rely on databases. For example, use increment for the primary key of userinfo this time.

    

At this time, if I execute the above insert method multiple times, no exception will be thrown because it is a primary key auto-increment. When specified as increment, hibernate first queries the largest id value in the database when inserting, then adds one to the id and inserts it again.
3. native and identity
Indicates the auto-increment of the primary key. hibernate automatically selects different implementation methods based on different databases.
4. hilo
Use a high/low-level algorithm to efficiently generate long, short, or int type identifiers, and specify a table and a field (hibernate_unique_key and next_hi by default) as the source of the high value, the identifier generated by the high/low algorithm is unique only in a specific database.

   

Here, we set the id generation policy to hilo. At this time, we perform the insert operation twice:

transaction = session.beginTransaction();UserInfo info = new UserInfo();info.setUserName("aa");info.setUserPass("vvv");session.save(info);transaction.commit();

At this time, hibernate generates an additional table for us: hibernate_unique_key in this table, a field next_hi

We can see that at this time, hibernate automatically generates a primary key for us.

As mentioned above, the primary key is maintained by default to indicate hibernate_unique_key, but we can also specify it ourselves.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: SQL;"> testprimary testcolumn

5. uuid
Uuid indicates that the primary key is generated for properties of the string type, and the generated primary key is also a unique string. For example, I changed the id of the userinfo object class to the string type. The primary key generation policy is specified as uuid.

    

The insert operation is performed multiple times:

6. Joint Primary Key Generation Policy
If you use the joint primary key generation policy, you cannot use two simple fields as before. At this time, you must use an object class that implements the Serializable interface.

package com.mydb.entity;import java.io.Serializable;public class ComposeKey implements Serializable { private int userId; private String userAddress; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((userAddress == null) ? 0 : userAddress.hashCode()); result = prime * result + userId; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ComposeKey other = (ComposeKey) obj; if (userAddress == null) { if (other.userAddress != null) return false; } else if (!userAddress.equals(other.userAddress)) return false; if (userId != other.userId) return false; return true; }}

We can see that here, I use the userId and userAddress fields to ensure the joint primary key. The main ComposeKey must implement the hashCode and equals methods, because hibernate uses these two methods to compare whether the two objects are equal, this guarantees the uniqueness of the federated primary key.
The UserInfo class is as follows:

package com.mydb.entity;import java.io.Serializable;public class UserInfo implements Serializable { private ComposeKey composeKey; private String userName; private String userPass; public ComposeKey getComposeKey() { return composeKey; } public void setComposeKey(ComposeKey composeKey) { this.composeKey = composeKey; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPass() { return userPass; } public void setUserPass(String userPass) { this.userPass = userPass; }}

The next step is to configure the federated primary key. UserInfo. hbm. xml

             

In this case, the insert operation is performed. Because the federated primary key is used, it is equivalent to the primary key specified by ourselves. The set needs to be displayed to set the primary key:

transaction = session.beginTransaction();UserInfo info = new UserInfo();info.setComposeKey(new ComposeKey(1,"china"));info.setUserName("aa");info.setUserPass("vvv");session.save(info);transaction.commit();


We can see that at this time, the attributes userId and userAddress are used together as the primary key in the userInfo table. At this point, if I insert an id that is info. setComposeKey (new ComposeKey (1, "china"); such data, hibernate will throw the following exception for us:

Property ing

For property ing, common attributes are as follows:
1. name: attribute name of the corresponding class
2. type: specifies the type of the attribute. Generally, this parameter can be left blank and automatically matched by hibernate.
3. length: specify the length.
4. column: Specifies the name of the database field corresponding to the attribute. If this parameter is not specified, it indicates the name of the attribute.
5. not-null: whether it is not null
6. unique: unique or not
7. update: whether to include this attribute when an update statement is issued
When you need to store a long text, such as a novel, you need to specify the type as "text", this type is only limited by the size of the hard disk.

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.