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

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


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

  1. 必须提供一个public的无参数构造函数。
  2. 必须实现序列化接口。
  3. 必须重写hashCode()和equals()这两个方法。这两个方法应该采用复合主键的字段作为判断这个对象是否相等的。
  4. 联合主键类的类名结尾一般要加上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 }

生成的联合主键如图:





转载于:https://www.cnblogs.com/lich/archive/2011/12/03/2274672.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值