JPA一对一关联

  这里我们仍然是使用annotation对实体进行配置。使用person与idcard模拟一对一的关联关系,一个人只能有一个ID号,同样一个ID号只能对应一个人,人与ID号是一对一的关联关系。Person类如下:

 1 package com.yichun.bean;

 2 

 3 import javax.persistence.CascadeType;

 4 import javax.persistence.Column;

 5 import javax.persistence.Entity;

 6 import javax.persistence.GeneratedValue;

 7 import javax.persistence.Id;

 8 import javax.persistence.JoinColumn;

 9 import javax.persistence.OneToOne;

10 import javax.persistence.Table;

11 

12 //关系的维护端

13 @Entity

14 @Table(name = "person")

15 public class Person {

16     private Integer id;

17     private String name;

18     private IDCard idcard;

19 

20     public Person() {

21     }

22 

23     public Person(String name) {

24         this.name = name;

25     }

26 

27     @Id

28     @GeneratedValue

29     public Integer getId() {

30         return id;

31     }

32 

33     public void setId(Integer id) {

34         this.id = id;

35     }

36 

37     @Column(length = 10, nullable = false)

38     public String getName() {

39         return name;

40     }

41 

42     public void setName(String name) {

43         this.name = name;

44     }

45 

46     @OneToOne(optional = false, cascade = CascadeType.ALL)//级联保存、修改、删除、同步

47     @JoinColumn(name = "idcard_id")// 外键的名称idcard_id

48     public IDCard getIdcard() {

49     return idcard;50     }   51 52     public void setIdcard(IDCard idcard) {53         this.idcard = idcard;

54     }

55 }

IDCard类如下:

 1 package com.yichun.bean;

 2 

 3 import javax.persistence.CascadeType;

 4 import javax.persistence.Column;

 5 import javax.persistence.Entity;

 6 import javax.persistence.GeneratedValue;

 7 import javax.persistence.Id;

 8 import javax.persistence.OneToOne;

 9 import javax.persistence.Table;

10 

11 //关系被维护端

12 @Entity

13 @Table(name = "idcard")

14 public class IDCard {

15     private Integer id;

16     private String cardno;

17     private Person person;

18 

19     public IDCard() {

20     }

21 

22     public IDCard(String cardno) {

23         this.cardno = cardno;

24     }

25 

26     @Id

27     @GeneratedValue

28     public Integer getId() {

29         return id;

30     }

31 

32     public void setId(Integer id) {

33         this.id = id;

34     }

35 

36     @Column(length = 18, nullable = false)

37     public String getCardno() {

38         return cardno;

39     }

40 

41     public void setCardno(String cardno) {

42         this.cardno = cardno;

43     }

44 

45     // 出现mappedBy的就是关系被维护端,由idCard维护, optional = false可以不设置

46     @OneToOne(mappedBy = "idcard", cascade = { CascadeType.PERSIST,

47             CascadeType.MERGE, CascadeType.REFRESH })//CascadeType.PERSIST 级联保存,CascadeType.MERGE 级联更新,CascadeType.REFRESH 级联刷新

48     public Person getPerson() {

49         return person;

50     }

51 

52     public void setPerson(Person person) {

53         this.person = person;

54     }

55 }

保存数据

 1     @Test

 2     public void save() {

 3         EntityManagerFactory factory = Persistence

 4                 .createEntityManagerFactory("testjpa");

 5         EntityManager manager = factory.createEntityManager();

 6         manager.getTransaction().begin();

 7 

 8         Person person = new Person("张三");

 9         IDCard idCard = new IDCard("1111122222");

10         person.setIdcard(idCard);

11         // 1、保存person

12         // 2、保存IDCard

13         // 3、使用IDCard的id作为外键保存到person里面

14         manager.persist(person);

15 

16         manager.getTransaction().commit();

17         manager.close();

18         factory.close();

19     }

 

你可能感兴趣的:(jpa)