JPA中的联合主键详解

两个或多个字段构成的主键,我们叫联结主键。在面临对象中,我们用JPA怎么定义这种状况呢?

怎么定义联结主键?用面临对象的思维来考虑的话,联结主键里的复合主键(字段),可以把它看作一个通体,其后采取一个主键种来描述这个复合主键的字段

对于联结主键种,大伙一定要信守以下几点JPA轨范:
一.务须提供一个public的无参数结构函数。
二.务必兑现序列化接口
三.务必重写hashCode()和equals()这两个步骤。这两个步骤应当采取复合主键的字段作为判断这个对像样否相等的


ArtLinePK.java

Java代码
一.package cn.itcast.bean;
2.
三.import java.io.Serializable;
4.
五.import javax.persistence.Column;
六.import javax.persistence.Embeddable;
7.
8.@Embeddable
9.//这个诠注代表ArtLinePK这个类是用在实业里头,告诉JPA的兑现产品:在实业类里头只是应用这类定义的属性
10.//容易的理解为:ArtLinePK里的属性可以看成是ArtLine种里的属性,打比方ArtLinePK的属性便是在ArtLine里定义的
11.public class ArtLinePK implements Serializable{
12. private String startCity;
13. private String endCity;
14.
15. public ArtLinePK() {
16. }
17.
18. public ArtLinePK(String startCity, String endCity) {
19. this.startCity = startCity;
20. this.endCity = endCity;
21. }
22.
23. @Column(length=三)
24. public String getStartCity() {
25. return startCity;
26. }
27.
28. public void setStartCity(String startCity) {
29. this.startCity = startCity;
30. }
31.
32. @Column(length=三)
33. public String getEndCity() {
34. return endCity;
35. }
36.
37. public void setEndCity(String endCity) {
38. this.endCity = endCity;
39. }
40.
41. @Override
42. public int hashCode() {
43. final int prime = 31;
44. int result = 一;
45. result = prime * result + ((endCity == null) ? 零 : endCity.hashCode());
46. result = prime * result
47. + ((startCity == null) ? 零 : startCity.hashCode());
48. return result;
49. }
50.
51. @Override
52. public boolean equals(Object obj) {
53. if (this == obj)
54. return true;
55. if (obj == null)
56. return false;
57. if (getClass() != obj.getClass())
58. return false;
59. final ArtLinePK other = (ArtLinePK) obj;
60. if (endCity == null) {
61. if (other.endCity != null)
62. return false;
63. } else if (!endCity.equals(other.endCity))
64. return false;
65. if (startCity == null) {
66. if (other.startCity != null)
67. return false;
68. } else if (!startCity.equals(other.startCity))
69. return false;
70. return true;
71. }
72.
73.}
package cn.itcast.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
//这个引语代表ArtLinePK这个类是用在实业里边,告诉JPA的兑现产品:在实业类里边只是施用这类定义的属性
//简略的理解为:ArtLinePK里的属性可以看成是ArtLine种里的属性,比作ArtLinePK的属性乃是在ArtLine里定义的
public class ArtLinePK implements Serializable{
private String startCity;
private String endCity;

public ArtLinePK() {
}

public ArtLinePK(String startCity, String endCity) {
this.startCity = startCity;
this.endCity = endCity;
}

@Column(length=三)
public String getStartCity() {
return startCity;
}

public void setStartCity(String startCity) {
this.startCity = startCity;
}

@Column(length=三)
public String getEndCity() {
return endCity;
}

public void setEndCity(String endCity) {
this.endCity = endCity;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 一;
result = prime * result + ((endCity == null) ? 零 : endCity.hashCode());
result = prime * result
+ ((startCity == null) ? 零 : startCity.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ArtLinePK other = (ArtLinePK) obj;
if (endCity == null) {
if (other.endCity != null)
return false;
} else if (!endCity.equals(other.endCity))
return false;
if (startCity == null) {
if (other.startCity != null)
return false;
} else if (!startCity.equals(other.startCity))
return false;
return true;
}

}
这个联结主键种,应当作为实业种的一个主键(实业标识符,id)

ArtLine.java

Java代码
一.package cn.itcast.bean;
2.
三.import javax.persistence.Column;
四.import javax.persistence.EmbeddedId;
五.import javax.persistence.Entity;
6.
7.@Entity
八.public class ArtLine {
9. private ArtLinePK id;//用面临对象的思维去思忖的话,这个复合主键看作一个通体,由复合主键种ArtLinePK来描述
10. private String name;
11.
12. public ArtLine() {
13. }
14.
15. public ArtLine(ArtLinePK id) {
16. this.id = id;
17. }
18.
19. public ArtLine(String startCity, String endCity, String name) {
20. this.id = new ArtLinePK(startCity, endCity);
21. this.name = name;
22. }
23.
24. @EmbeddedId //依照JPA轨范要求,我们并不是用@Id来标注它
25. //@EmbeddedId 这个引文用以标注id这个属性为实业的标识符,由于我们施用的是复合主键种,
26. //之所以我们要用@EmbeddedId这个专程针对复合主键种的 用以标示实业标识符的注脚
27. public ArtLinePK getId() {
28. return id;
29. }
30.
31. public void setId(ArtLinePK 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.}
package cn.itcast.bean;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;

@Entity
public class ArtLine {
private ArtLinePK id;//用面临对象的思维去考虑的话,这个复合主键看作一个通体,由复合主键种ArtLinePK来描述
private String name;

public ArtLine() {
}

public ArtLine(ArtLinePK id) {
this.id = id;
}

public ArtLine(String startCity, String endCity, String name) {
this.id = new ArtLinePK(startCity, endCity);
this.name = name;
}

@EmbeddedId //依照JPA轨范要求,我们并不是用@Id来标注它
//@EmbeddedId 这个引文用以标注id这个属性为实业的标识符,由于我们运用的是复合主键种,
//之所以我们要用@EmbeddedId这个专程针对复合主键种的 用来标示实业标识符的引文
public ArtLinePK getId() {
return id;
}

public void setId(ArtLinePK id) {
this.id = id;
}

@Column(length=20)
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
我们的复合主键种,目前为止,已经定义好了。定义好了以后,接着我们就看它生成的数据库表是不是满足复合主键这么一种状况。

ArtLineTest.java

Java代码
一.package junit.test;
2.
3.
四.import javax.persistence.EntityManager;
五.import javax.persistence.EntityManagerFactory;
六.import javax.persistence.Persistence;
7.
八.import org.junit.BeforeClass;
九.import org.junit.Test;
10.
11.import cn.itcast.bean.ArtLine;
12.
13.public class ArtLineTest {
14.
15. @BeforeClass
16. public static void setUpBeforeClass() throws Exception {
17. }
18.
19. @Test public void save(){
20. EntityManagerFactory factory = Persistence
21. .createEntityManagerFactory("itcast");
22. EntityManager em = factory.createEntityManager();
23. em.getTransaction().begin();
24.
25. em.persist(new ArtLine("PEK","SHA","北京市飞上海市"));
26.
27. em.getTransaction().commit();
28. em.close();
29. factory.close();
30. }
31.}
package junit.test;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.BeforeClass;
import org.junit.Test;

import cn.itcast.bean.ArtLine;

public class ArtLineTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Test public void save(){
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();

em.persist(new ArtLine("PEK","SHA","北京市飞上海市"));

em.getTransaction().commit();
em.close();
factory.close();
}
}

字段生成了,沒問題。。看图:


视主键,两个字段,看图:契合主键的定义便OK了,


数据也平添进去了,看图:

本文来源:
我的异常网
Java Exception
Dotnet Exception
Oracle Exception

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值