您的位置:首页 > 其它

两个属性定义一个主键:JPA中的联合主键

2013-04-02 10:50 639 查看


JPA基础(十三):JPA中的联合主键

两个或多个字段组成的主键,我们叫联合主键。在面向对象中,我们用JPA怎么定义这种情况呢?怎么定义联合主键?用面向对象的思想来思考的话,联合主键里的复合主键(字段),可以把它看成一个整体,然后采用一个主键类来描述这个复合主键的字段。

这里面主要是一些类的主键是联合主键, 主键是有两个属性构成的

关于联合主键类,大家一定要遵守以下几点JPA规范:

必须提供一个public的无参数构造函数。
必须实现序列化接口。
必须重写hashCode()和equals()这两个方法。这两个方法应该采用复合主键的字段作为判断这个对象是否相等的。
联合主键类的类名结尾一般要加上PK两个字母代表一个主键类,不是要求而是一种命名风格。

AirLinePK.java:

1 @Embeddable
2 //这个注解代表ArtLinePK这个类是用在实体里面,告诉JPA的实现产品:在实体类里面只是使用这个类定义的属性。
3 //简单的理解为:ArtLinePK里的属性可以看成是ArtLine类里的属性,好比ArtLinePK的属性就是在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     //这个注解用于标注id这个属性为实体的标识符,因为我们使用的是复合主键类,所以我们要用@EmbeddedId这个专门针对复合主键类的标志实体标识符的注解。
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 }


测试:

1 public class AirLineTest {
2     @Test
3     public void save(){
4         EntityManagerFactory factory=Persistence.createEntityManagerFactory("sample");//形如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("上海飞北京");
13         em.persist(airline);
14         em.getTransaction().commit();
15         em.close();
16         factory.close();
17     }
18 }


生成的联合主键如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐