(ix) Hibernate's Composite primary key

Source: Internet
Author: User

First, what is a composite primary key?
    • The primary key of a table is made up of multiple fields, which are called composite primary keys. A primary key is the only field that determines a record, such as a citizen's ID is a primary key, because the primary key is determined by the ID number.
    • A citizen. There is a situation, such as student performance in this table, a school number can not be the only certainty of the student's performance, because the student has a multi-disciplinary results, this time, "study number + discipline" is the primary key, the combination of the two can only determine a record, that is, the results.

Hibernate generates a mapping bean containing a composite primary key table

2.1 Creating a table with a composite primary key

This table has a composite primary key

2.2 Generating the mapping file

Hibernate generates a bean from a table containing a composite primary key independently of the composite primary key as a separate bean.

2.3 Analyzing score and Scoreid beans

2.4 Analyzing the total Map file

Third, for the bean with the primary key to increase the deletion and check operation

Case one Test_compositeid. java

 PackageAction;Importjava.io.Serializable;Importorg.hibernate.Session;Importorg.hibernate.Transaction;ImportBean. Score;ImportBean. Scoreid;Importutil. Hibernateutil; Public classTest_compositeid { Public Static voidMain (string[] args) {//Test_compositeid.select (); //Test_compositeid.save ();test_compositeid.update (); }    /*** Find*/    Private Static voidSelect () {Session session=NULL; Transaction Tran=NULL; Scoreid ID=NewScoreid (); Id.setuserid (1); Id.setsubject (Language); Try{Session=hibernateutil.getsessionfactory (). Getcurrentsession (); Tran=session.begintransaction (); Score Score= (score) Session.get (score.class, id); System.out.println ("Score =" +Score.getscore ());        Tran.commit (); } Catch(Exception e) {e.printstacktrace ();        Tran.rollback (); }    }    Private Static voidSave () {Session session=NULL; Transaction Tran=NULL; Scoreid ID=NewScoreid (); Id.setsubject (English); Id.setuserid (1); Score Score=Newscore ();        Score.setid (ID); Score.setscore (100); Try{Session=hibernateutil.getsessionfactory (). Getcurrentsession (); Tran=session.begintransaction (); Serializable PrimaryKey=Session.save (score);            System.out.println (PrimaryKey);        Tran.commit (); } Catch(Exception e) {e.printstacktrace ();        Tran.rollback (); }    }    /*** Perform update operations*/    Private Static voidUpdate () {Session session=NULL; Transaction Tran=NULL; Scoreid ID=NewScoreid (); Id.setuserid (1); Id.setsubject (English); Score Score=Newscore ();        Score.setid (ID); Score.setscore (12); Try{Session=hibernateutil.getsessionfactory (). Getcurrentsession (); Tran=session.begintransaction ();                                    Session.update (score);        Tran.commit (); } Catch(Exception e) {e.printstacktrace ();        Tran.rollback (); }    }}

All codes are here: Links

(ix) Hibernate's Composite primary key

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.