7.3 Hibernate: Built-in builder--GUID

Source: Internet
Author: User
Tags class definition generator local time uuid

Uses a GUID generated by a database of type string.

guid:globally Unique Identifier, a globally unique identifier, also known as a UUID, is a 128-bit long number, expressed as a 16 binary. The core idea of the algorithm is to combine the machine's NIC, local time, and a random number to generate GUIDs.

Hibernate queries the database before maintaining the primary key, obtaining a UUID string, the primary key value. This value is unique, with the disadvantage of being large in length and limited in supported databases, with the benefit of cross-database but still requiring access to the database.

Note: The length varies depending on the database. MySQL uses the Select UUID () statement to get a 36-bit string (standard format containing "-") using the Select Rawtohex (Sys_guid ()) from DUAL statement in Oracle to obtain a 32-bit string (not Include "-")

Features: Need database support query UUID, generate the need to query the database, efficiency without UUID high, recommended to use UUID.

Using MySQL Demo: 1 using XML

1.1 Persistence class Definition:

Package hibernate;

Import Java.util.Date;

    public class Person {private String ID;

    Private String account;

    private String name;

    Private Date birth;
        Public person () {} public person (string account, string name, Date birth) {this.account = account;
        THIS.name = name;
    This.birth = birth;
    } public String GetId () {return id;
    } public void SetId (String id) {this.id = ID;
    } public String Getaccount () {return account;
    The public void Setaccount (String account) {this.account = account;
    } public String GetName () {return name;
    } public void SetName (String name) {this.name = name;
    Public Date Getbirth () {return birth;
    } public void Setbirth (Date birth) {This.birth = birth; } @Override Public String toString () {return ' person [id= + ID + ', account= ' + account + ', name= "+ N Ame+ ", birth=" + Birth + "]"; }

}

Note: The GUID generated by MySQL is a 36-bit standard UUID string with "-", so the persisted class primary key is defined as a string type, and the mapping file needs to be consistent.

1.2 Defining Mappings:

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

1.3 Unit Test:

package hibernate;

Import Java.util.Date;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;

Import Org.junit.Test; public class Hibernatetest {@Test public void Test () {Configuration configuration = new configuration ()
        . Configure ("Hibernate.cfg.xml");
        Sessionfactory sessionfactory = Configuration.buildsessionfactory ();
        Session session = Sessionfactory.opensession ();
        Transaction Transaction = Session.begintransaction ();
        Date date = new Date (System.currenttimemillis ());
        Person Person1 = new Person ("Admin1", "Nick", date);
        Session.save (Person1);
        Person Person2 = new Person ("admin2", "King", date);
        Session.save (Person2);
        Transaction.commit ();
        Session.close ();
    Sessionfactory.close (); }

}

The unit test passes, querying the database for newly inserted data:
2 using annotations (annotation)

To define a persistence class using annotations:

Package hibernate;

Import Java.util.Date;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;

Import javax.persistence.Table;

Import Org.hibernate.annotations.GenericGenerator; @Entity @Table public class Person {@Id @GeneratedValue (generator = "Assignedgenerator") @GenericGenerator (n

    Ame = "Assignedgenerator", strategy = "GUID") private String ID;

    Private String account;

    private String name;

    Private Date birth;
        Public person () {} public person (string account, string name, Date birth) {this.account = account;
        THIS.name = name;
    This.birth = birth;
    } public String GetId () {return id;
    } public void SetId (String id) {this.id = ID;
    } public String Getaccount () {return account;
    The public void Setaccount (String account) {this.account = account;
 } public String GetName () {return name;   } public void SetName (String name) {this.name = name;
    Public Date Getbirth () {return birth;
    } public void Setbirth (Date birth) {This.birth = birth; } @Override Public String toString () {return ' person [id= + ID + ', account= ' + account + ', name= "+ N
    Ame + ", birth=" + Birth + "]"; }

}

Run the 1.3 unit test and the test results are the same.

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.