聯合主鍵也就是說需要多個字段才能確定數據庫記錄中的唯一一行。這樣就需要多個字段一起,組成主鍵,也叫聯合主鍵。例如飛機航線,我們需要知道飛機起飛的地點以及飛機降落的地點。所以需要飛機起飛的地點和降落的地方才能確定一條航線。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 }