Ing hibernate object identifier

Source: Internet
Author: User

Ing Hibernate Object Identifier: Hibernate The Persistent Object in corresponds to a data table in the database. Therefore, distinguishing different persistent objects cannot be like Java Method passed Object Default object Equals () In Hibernate Is passed Oid To complete, Oid Corresponds to the primary key in the database.   Next I will explain Hibernate In Hibernate In total 8 Identifier generation method, including 7 Identifier generator and compound primary key generation method. (1) Increment Identifier generator: Configuration method: <Hibernate-mapping> <Class name = "incrementtester" table = "increment_tester"> <ID name = "ID" type = "long" column = "ID"> <Generator class = "increment"/> </ID> </Class> </Hibernate-mapping> Use this method to generate Oid , Hibernate It is generated incrementally when an object is persisted. Oid This method does not depend on the underlying database, and is only applicable to but in the process environment, the same primary key value may be generated in the multi-threaded environment, and Oid Must be Long, Int, short Type. (2) Identify Identifier generator: Configuration method: <Hibernate-mapping> <Class name = "identifytester" table = "iidentify_tester"> <ID name = "ID" type = "long" column = "ID"> <Generator class = "identify"/> </ID> </Class> </Hibernate-mapping> Identify The identifier generation mechanism depends on the support of the underlying database. Therefore, the underlying database system must support automatic field growth. , And Oid Must be Long, Int, short Type. (3) Sequence Identifier generator: Configuration method: <Hibernate-mapping>   .... <ID name = "ID" type = "long" column = "ID"> <Generator class = "sequence"> <Param name = "sequence"> tester_id_seq </param> </Generator> </ID> .... </Hibernate-mapping> By Sequence The generation mechanism of identifiers depends on the sequence of the underlying database system. Therefore, the database must support the sequence mechanism (for example: Oracle ), And Oid Must be Long, Int, short Type. (4) HiLo Identifier generator:   Configuration method: <Hibernate-mapping> .... <ID name = "ID" type = "long" column = "ID"> <Generator class = "HiLo"> <Param name = "table"> hi_value </param> <Param name = "column"> next_value </param> <Param name = "max_lo"> 100 </param> </Generator> </ID> .... </Hibernate-mapping> HiLo Identifier generator Hibernate According High/low Algorithm generation identifier, which is obtained from the fields of a specific table in the database High Therefore, an additional database table is required to save the historical status generated by the primary key, HiLo The generation method does not depend on the underlying database, so it is applicable to each type of database, Oid Must be Long, Int, short Type. (5) Native Identifier generator: Configuration method: <Hibernate-mapping> .... <ID name = "ID" type = "long" column = "ID"> <Generator class = "native"/> </ID> .... </Hibernate-mapping> Native The generator can automatically select an appropriate identifier generator based on the type of the underlying database system, so it is very suitable for cross-database platform development. Hibernate Automatically uses Identify, Hilo, sequence One of them is used as the primary key generation method, Oid Must be Long, Int, short Type. (6) UUID. Sex Identifier generator: Configuration method: <Hibernate-mapping> .... <ID name = "ID" type = "long" column = "ID"> <Generator class = "UUID. Sex"/> </ID> .... </Hibernate-mapping>   By Hibernate Based on 128 Bit unique value generation algorithm, based on the current device IP , Time, JVM Start time, internal auto increment, etc. 4 Parameter generation 16 The hexadecimal value is used as the primary key. UUID. Sex The primary key generated in this way provides the best data insertion performance and database platform adaptability. (7) Assign Identifier generator: <Hibernate-mapping> .... <ID name = "ID" type = "long" column = "ID"> <Generator class = "assign"/> </ID> .... </Hibernate-mapping> Use Assign The generation policy indicates that the application logic is responsible for generating the primary key identifier. (8) Ing compound primary keys The first method is composed of attributes in the Entity class. The entity class itself assumes the role of the composite primary key class.   Configuration method: <Hibernate-mapping> <Class name = "customer" table = "customer"> <Composite-ID> <Key-property name = "name" column = "name" type = "string"/> <Key-property name = "compayid" column = "companyid" type = "string"/> </Composite-ID> </Class> </Hibernate-mapping> To use Session. Load () Method to load the object. The object must be implemented. Java. Io. serializable Interface , And provide Equals () Method and Hashcode () Method implementation. Example: Public Class Customer implements serilizable { Private final string name; Private final string companyid; ....... Public Boolean equals (Object O ){ If (this = O) return false; If (! (O instanceof customer) return false; Customer Other = (customer) O; If (! Name. Equals (other. getname () return false; If (! Companyid. Equals (other. Get companyid () return false; Return true;   } Public int hashcode (){ Int result = (name = NULL )? 0: Name. hascode (); Result = 29 * result + (companyid = NULL? 0: companyid. hascode ()); Return result; } }   Customer c = new customer (); C. setname ("zx "); C. setcompanyid ("Neusoft "); Session. Load (customer. Class, C );   Method 2 : You can map a composite primary key with an independent primary key class to isolate the logic gradually.   Configuration method :   <Hibernate-mapping> <Class name = "customer" table = "customer"> <Composite-ID name = "customerid" class = "mypack. customerid"> <Key-property name = "name" column = "name" type = "string"/> <Key-property name = "compayid" column = "companyid" type = "string"/> </Composite-ID> </Class> </Hibernate-mapping>   Primary Key class: Public class customerid { Private string name; Private string customerid; ....... }   Entity class: Public Class Customer { Private customerid CID; Private string address; ........ } Customerid = new customerid (); Customerid. setname ("zx "); Customerid. setsustomerid ("Neusoft "); Customer c = (customer) Session. Load (customer. Class, customerid ); 

 

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.