JPA联合主键

  联合主键也就是说需要多个字段才能确定数据库记录中的唯一一行。这样就需要多个字段一起,组成主键,也叫联合主键。例如飞机航线,我们需要知道飞机起飞的地点以及飞机降落的地点。所以需要飞机起飞的地点和降落的地方才能确定一条航线。AirLine表示航线,AirLinePK表示主键类。AirLinePK代码如下:

 1 package com.yichun.bean;
 2 
 3 import java.io.Serializable;
 4 
 5 import javax.persistence.Column;
 6 import javax.persistence.Embeddable;
 7 
 8 /**
 9  * 联合主键。一般使用PK 只需要定义用作主键的字段
10  * <p>
11  * 联合主键类必须遵守的JPA规范:<br>
12  * 1、必须要提供一个public的无参数的构造方法<br>
13  * 2、必须要实现序列化接口<br>
14  * 3、必须重写hashCode()与equals()方法
15  */
16 // 用在实体里面,只是使用该类中的属性。(该类中的属性用在持久化的类中的字段)
17 @Embeddable
18 public class AirLinePK implements Serializable {
19     private String startCity;// PEK,北京 CAN广州,SHA上海
20     private String endCity;
21 
22     public AirLinePK() {
23     }
24 
25     public AirLinePK(String startCity, String endCity) {
26         this.startCity = startCity;
27         this.endCity = endCity;
28     }
29 
30     @Column(length = 3, nullable = false)
31     public String getStartCity() {
32         return startCity;
33     }
34 
35     public void setStartCity(String startCity) {
36         this.startCity = startCity;
37     }
38 
39     @Column(length = 3, nullable = false)
40     public String getEndCity() {
41         return endCity;
42     }
43 
44     public void setEndCity(String endCity) {
45         this.endCity = endCity;
46     }
47 
48     // 以下两个方法判断是否相等
49     @Override
50     public int hashCode() {
51         final int prime = 31;
52         int result = 1;
53         result = prime * result + ((endCity == null) ? 0 : endCity.hashCode());
54         result = prime * result
55                 + ((startCity == null) ? 0 : startCity.hashCode());
56         return result;
57     }
58 
59     @Override
60     public boolean equals(Object obj) {
61         if (this == obj)
62             return true;
63         if (obj == null)
64             return false;
65         if (getClass() != obj.getClass())
66             return false;
67         AirLinePK other = (AirLinePK) obj;
68         if (endCity == null) {
69             if (other.endCity != null)
70                 return false;
71         } else if (!endCity.equals(other.endCity))
72             return false;
73         if (startCity == null) {
74             if (other.startCity != null)
75                 return false;
76         } else if (!startCity.equals(other.startCity))
77             return false;
78         return true;
79     }
80 
81 }

  AirLine 代码如下:

 1 package com.yichun.bean;
 2 
 3 import javax.persistence.Column;
 4 import javax.persistence.EmbeddedId;
 5 import javax.persistence.Entity;
 6 
 7 
 8 @Entity
 9 public class AirLine {
10     private AirLinePK id;
11     private String name;
12 
13     public AirLine() {
14     }
15 
16     public AirLine(AirLinePK id) {
17         this.id = id;
18     }
19 
20     public AirLine(String startCity, String endCity, String name) {
21         this.id = new AirLinePK(startCity, endCity);
22         this.name = name;
23     }
24 
25     // 用于标识该属性为实体的标识符,专门用于复合主键类
26     @EmbeddedId
27     public AirLinePK getId() {
28         return id;
29     }
30 
31     public void setId(AirLinePK id) {
32         this.id = id;
33     }
34 
35     @Column(length = 20)
36     public String getName() {
37         return name;
38     }
39 
40     public void setName(String name) {
41         this.name = name;
42     }
43 
44 }

保存数据如下:

 1     @Test
 2     public void save() {
 3         EntityManagerFactory factory = Persistence
 4                 .createEntityManagerFactory("yichun");
 5         EntityManager manager = factory.createEntityManager();
 6         manager.getTransaction().begin();
 7 
 8         manager.persist(new AirLine("PEK", "SHA", "北京飞上海"));
 9 
10         manager.getTransaction().commit();
11         manager.close();
12         factory.close();
13     }

查找数据如下:

 1 @Test
 2     public void find() {
 3         EntityManagerFactory factory = Persistence
 4                 .createEntityManagerFactory("yichun");
 5         EntityManager manager = factory.createEntityManager();
 6 
 7         AirLine airLine = manager.find(AirLine.class, new AirLinePK("PEK",
 8                 "SHA"));
 9         System.out.println(airLine.getName() + " : "
10                 + airLine.getId().getStartCity() + " --> "
11                 + airLine.getId().getEndCity());
12 
13         manager.close();
14         factory.close();
15     }

 

posted on 2013-12-10 16:16  烟火_  阅读(4228)  评论(0编辑  收藏  举报

导航