Hibernate Framework Learning Note 4: Primary key generation policy, object state

Source: Internet
Author: User
Tags lenovo

Create an entity class:

 Packagedomain; Public classCustomer {PrivateLong cust_id; PrivateString Cust_name; PrivateString Cust_source; PrivateString Cust_industry; PrivateString Cust_level; PrivateString Cust_linkman; PrivateString Cust_phone; PrivateString Cust_mobile;  PublicLong getcust_id () {returncust_id; }     Public voidsetcust_id (Long cust_id) { This. cust_id =cust_id; }     PublicString Getcust_name () {returnCust_name; }     Public voidsetcust_name (String cust_name) { This. Cust_name =Cust_name; }     PublicString Getcust_source () {returnCust_source; }     Public voidSetcust_source (String cust_source) { This. Cust_source =Cust_source; }     PublicString getcust_industry () {returnCust_industry; }     Public voidsetcust_industry (String cust_industry) { This. Cust_industry =Cust_industry; }     PublicString Getcust_level () {returnCust_level; }     Public voidsetcust_level (String cust_level) { This. Cust_level =Cust_level; }     PublicString Getcust_linkman () {returnCust_linkman; }     Public voidSetcust_linkman (String cust_linkman) { This. Cust_linkman =Cust_linkman; }     PublicString Getcust_phone () {returnCust_phone; }     Public voidSetcust_phone (String cust_phone) { This. Cust_phone =Cust_phone; }     PublicString Getcust_mobile () {returnCust_mobile; }     Public voidsetcust_mobile (String cust_mobile) { This. Cust_mobile =Cust_mobile; } @Override PublicString toString () {return"Customer [cust_id=" + cust_id + ", cust_name=" + Cust_name + "]"; }}
View Code

ORM Meta-Data configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd ">   <!--Configure the relationship between a table and an entity object -<hibernate-mapping>    <classname= "domain. Customer "Table= "Cst_customer" >        <IDname= "cust_id"  >            <!--Generator: Primary key generation Policy -            <Generatorclass= "Native"></Generator>        </ID>        < Propertyname= "Cust_name"column= "Cust_name" ></ Property>        < Propertyname= "Cust_source"column= "Cust_source" ></ Property>        < Propertyname= "Cust_industry"column= "Cust_industry" ></ Property>        < Propertyname= "Cust_level"column= "Cust_level" ></ Property>        < Propertyname= "Cust_linkman"column= "Cust_linkman" ></ Property>        < Propertyname= "Cust_phone"column= "Cust_phone" ></ Property>        < Propertyname= "Cust_mobile"column= "Cust_mobile" ></ Property>    </class></hibernate-mapping>

The primary key generation strategy here is native

With:

Primary key generation policy. Is the generation rule of the primary key when each record is entered. (7)


Identity: Primary key self-increment. The primary key value is maintained by the database. You do not need to specify a primary key when entering.


The primary key generation strategy in Sequence:oracle, where MySQL is used, temporarily does not introduce


Increment (not used, inefficient, and thread-safe issues): primary key self-increment. It is maintained by hibernate. The maximum ID value in the table is queried before each insert. +1 as the new primary key value.


Hilo (not used, understood): High-low algorithm, primary key self-increment. It is maintained by hibernate and is not used during development.


Native:hilo+sequence+identity automatic three-choice policy (detects the current database, using the appropriate policy)


UUID: Produces a random string that theoretically never repeats as a primary key. The primary key type must be of type string.


Assigned: Natural primary key generation strategy. Hibernate does not manage the primary key values, which are entered by the developer themselves.

Hibernate objects are divided into three states:

transient status : No ID, no session Association

Persistent state : with ID, associated with session

Free State ( managed state ): ID, not associated with session

 PackageState ;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration;Importorg.junit.Test;ImportCn.itheima.domain.Customer;Importcn.itheima.utils.HibernateUtils;//three state of the test object Public classDemo {@Test//View three statuses     Public voidfun1 () {//1 Getting sessionSession session =hibernateutils.opensession (); //2 Control TransactionsTransaction tx =session.begintransaction (); //3 Performing ActionsCustomer C =NewCustomer ();//no ID, no associated with session--instantaneous stateC.setcust_name ("Lenovo");//instantaneous StateSession.save (c);//persistent state, with ID, associated//4 Commit the transaction. Close ResourceTx.commit (); Session.close ();//Free | Managed state, with ID, no association} @Test//three types of state features//Save Method: Actually can not understand to save. A method of translating transient state into a persistent state//PRIMARY Key increment: Executes the Save method in order to convert the object to a persisted state. ID values must be generated. Therefore, an INSERT statement generation is required. //If the primary key generation policy isIncrement: Executes the Save method in order to generate the ID. The SQL statement (select MAX (ID) from table) that executes the query ID maximum value .     Public voidfun2 () {//1 Getting sessionSession session =hibernateutils.opensession (); //2 Control TransactionsTransaction tx =session.begintransaction (); //3 Performing ActionsCustomer C =NewCustomer ();//no ID, no associated with session--instantaneous stateC.setcust_name ("Lenovo");//instantaneous StateSession.save (c);//persistent state, with ID, associated//4 Commit the transaction. Close ResourceTx.commit (); Session.close ();//Free | Managed state, with ID, no association} @Test//three types of state features//Persistence State feature: Any changes to the persisted state object are automatically synchronized to the database.     Public voidFun3 () {//1 Getting sessionSession session =hibernateutils.opensession (); //2 Control TransactionsTransaction tx =session.begintransaction (); //3 Performing ActionsCustomer C= Session.get (Customer.class, 1l);//persisted state ObjectC.setcust_name ("Microsoft Corporation"); //4 Commit the transaction. Close ResourceTx.commit (); Session.close ();//Free | Managed state, with ID, no association                    }}

Next, analyze Hibernate's additions and deletions from the state perspective:

Hibernate Framework Learning Note 4: Primary key generation policy, object state

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.