Jpa basics (13): joint primary keys in JPA

Source: Internet
Author: User

A primary key composed of two or more fields. We call it a joint primary key. In object-oriented systems, how do we define this situation using JPA? How to define the Union primary key? If we use the object-oriented thinking, the composite primary key (field) in the primary key can be considered as a whole, and then a primary key class is used to describe the field of the composite primary key.

AboutFederated primary key classTo comply with the following JPA specifications:

    1. A public non-parameter constructor must be provided.
    2. The serialization interface must be implemented.
    3. The hashcode () and equals () methods must be overwritten. The two methods should use fields of the composite primary key to determine whether the object is equal.
    4. Generally, the end of the Union primary key class name must contain two primary key letters to indicate a primary key class. This is not a requirement but a naming style.

Airlinepk. Java:

 1 @ Embeddable
2 // This annotation indicates that the artlinepk class is used in the entity and tells the implementation product of JPA that only attributes defined by this class are used in the Entity class.
3 // The attribute in artlinepk can be regarded as an attribute in the artline class. For example, the attribute of artlinepk is defined in artline.
4 Public Class Airlinepk Implements Serializable {
5 @ Column (length = 3)
6 Private String startcity;
7 @ Column (length = 3)
8 Private String endcity;
9 Public String getstartcity (){
10 Return Startcity;
11 }
12 Public Void Setstartcity (string startcity ){
13 This . Startcity = startcity;
14 }
15 Public String getendcity (){
16 Return Endcity;
17 }
18 Public Void Setendcity (string endcity ){
19 This . Endcity = endcity;
20 }
21 @ Override
22 Public Int Hashcode (){
23 Final Int Prime = 31;
24 Int Result = 1;
25 Result = prime * result + (endcity = Null )? 0: endcity. hashcode ());
26 Result = prime * result
27 + (Startcity =Null )? 0: startcity. hashcode ());
28 Return Result;
29 }
30 @ Override
31 Public Boolean Equals (Object OBJ ){
32 If ( This = OBJ)
33 Return True ;
34 If (OBJ = Null )
35 Return False ;
36 If (Getclass ()! = Obj. getclass ())
37 Return False ;
38 Airlinepk Other = (airlinepk) OBJ;
39 If (Endcity = Null ){
40 If (Other. endcity! = Null )
41 Return False ;
42 } Else If (! Endcity. Equals (other. endcity ))
43 Return False ;
44 If (Startcity = Null ){
45 If (Other. startcity! = Null )
46 Return False ;
47 } Else If (! Startcity. Equals (other. startcity ))
48 Return False ;
49 Return True ;
50 }
51
52 }

Airline. Java:

 1 @ Entity
2 Public Class Airline {
3 @ Embeddedid
4 // This annotation is used to mark the property ID as the identifier of the object. Because we use a composite primary key class, we need to use the annotation of @ embeddedid, a Mark entity identifier for the composite primary key class.
5 Private Airlinepk ID;
6 @ Column (length = 20)
7 Private String name;
8 Public Airlinepk GETID (){
9 Return ID;
10 }
11 Public Void Setid (airlinepk ID ){
12 This . ID = ID;
13 }
14 Public String getname (){
15 Return Name;
16 }
17 Public Void Setname (string name ){
18 This . Name = Name;
19 }
20
21 }

Test:

 1   Public  Class Airlinetest {
2 @ Test
3 Public Void Save (){
4 Entitymanagerfactory factory = persistence. createentitymanagerfactory ("sample "); // Like sessionfactory
5 Entitymanager em = factory. createentitymanager ();
6 Em. gettransaction (). Begin ();
7 Airlinepk id = New Airlinepk ();
8 Id. setstartcity ("Sha ");
9 Id. setendcity ("pek ");
10 Airline airline = New Airline ();
11 Airline. setid (ID );
12 Airline. setname ("Shanghai Flying Beijing ");
13 Em. persist (airline );
14 Em. gettransaction (). Commit ();
15 Em. Close ();
16 Factory. Close ();
17 }
18 }

Union primary key generated

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.