JPA关系映射之one-to-one

一对一关联有两种实现方式:一种是共享的主键关联,另一种是一对一的外键关联

1、共享的主键关联:让两个对象具有共同的主键值,以表明他们之间的一一对应关系。

 

Person.java类

 1 public class Person implements java.io.Serializable {
 2 
 3     // Fields
 4 
 5     private Integer id;
 6     private String name;
 7     private Idcard idcard;
 8 
 9     // Constructors
10 
11     /** default constructor */
12     public Person() {
13     }
14 
15     /** full constructor */
16     public Person(String name) {
17         this.name = name;
18     }
19 
20     // Property accessors
21 
22     public Integer getId() {
23         return this.id;
24     }
25 
26     public void setId(Integer id) {
27         this.id = id;
28     }
29 
30     public String getName() {
31         return this.name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37     public Idcard getIdcard() {
38         return idcard;
39     }
40 
41     public void setIdcard(Idcard idcard) {
42         this.idcard = idcard;
43     }
44 }

Idcard.java类

 1 public class Idcard implements java.io.Serializable {
 2 
 3     // Fields
 4 
 5     private Integer id;
 6     private String idcardno;
 7     private Person person;
 8 
 9     // Constructors
10 
11     /** default constructor */
12     public Idcard() {
13     }
14 
15     /** full constructor */
16     public Idcard(String idcardno) {
17         this.idcardno = idcardno;
18     }
19 
20     // Property accessors
21 
22     public Integer getId() {
23         return this.id;
24     }
25 
26     public void setId(Integer id) {
27         this.id = id;
28     }
29 
30     public String getIdcardno() {
31         return this.idcardno;
32     }
33 
34     public void setIdcardno(String idcardno) {
35         this.idcardno = idcardno;
36     }
37 
38     public Person getPerson() {
39         return person;
40     }
41 
42     public void setPerson(Person person) {
43         this.person = person;
44     }
45 
46 }

Person.hbm.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 4 <!-- 
 5     Mapping file autogenerated by MyEclipse Persistence Tools
 6 -->
 7 <hibernate-mapping>
 8     <class name="com.db.entity.Person" table="person" catalog="mydb">
 9         <id name="id" type="java.lang.Integer">
10             <column name="id" />
11             <generator class="native" />
12         </id>
13         <property name="name" type="java.lang.String">
14             <column name="name" length="30" />
15         </property>
16         <one-to-one name="idcard" class="com.db.entity.Idcard" cascade="all" />
17     </class>
18 </hibernate-mapping>

Idcard.hbm.xml

 1 <hibernate-mapping>
 2     <class name="com.db.entity.Idcard" table="idcard" catalog="mydb">
 3         <id name="id" type="java.lang.Integer">
 4             <column name="id" />
 5             <generator class="foreign">
 6             <param name="property">person</param>
 7             </generator>
 8         </id>
 9         <one-to-one name="person" constrained="true" class="com.db.entity.Person"></one-to-one>
10         <property name="idcardno" type="java.lang.String">
11             <column name="idcardno" length="20" />
12         </property>
13     </class>
14 </hibernate-mapping>

bibernate.cfg.xml

 1 <?xml version='1.0' encoding='UTF-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4           "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <!-- Generated by MyEclipse Hibernate Tools.                   -->
 6 <hibernate-configuration>
 7 
 8     <session-factory>
 9         <property name="dialect">
10             org.hibernate.dialect.MySQLDialect
11         </property>
12         <property name="connection.url">
13             jdbc:mysql://localhost:3306/mydb
14         </property>
15         <property name="connection.username">root</property>
16         <property name="connection.password">123456</property>
17         <property name="connection.driver_class">
18             com.mysql.jdbc.Driver
19         </property>
20         <property name="myeclipse.connection.profile">
21             MyDBAccount
22         </property>
23         <mapping resource="com/db/entity/Idcard.hbm.xml" />
24         <mapping resource="com/db/entity/Person.hbm.xml" />
25     </session-factory>
26 
27 </hibernate-configuration>

测试代码:

 1 public class TestOneOne {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5 
 6         Session session=null;
 7         session=HibernateSessionFactory.getSession();
 8         session.beginTransaction();
 9         Idcard idcard=new Idcard();
10         idcard.setIdcardno("2345678901");
11         Person person=new Person();
12         person.setName("Lily");
13         person.setIdcard(idcard);
14         idcard.setPerson(person);
15         session.save(person);
16         session.save(idcard);
17         session.getTransaction().commit();
18         
19     }
20 
21 }

2、一对一的外键关联:外键关联,本来是用于多对一的配置,但是加上唯一的限制之后(采用<many-to-one>标签来映射,指定多的一端unique为true,这样就限制了多的一端的多重性为一),也可以用来表示一对一关联关系,其实它就是多对一的特殊情况。

首先在表idcard中新添加一列pid用于共享数据

Person.java类

 1 public class Person implements java.io.Serializable {
 2 
 3     // Fields
 4 
 5     private Integer pid;
 6     private String name;
 7     private Idcard idcard;
 8 
 9     // Constructors
10 
11     /** default constructor */
12     public Person() {
13     }
14 
15     /** full constructor */
16     public Person(String name) {
17         this.name = name;
18     }
19 
20     // Property accessors
21 
22     public Integer getPid() {
23         return this.pid;
24     }
25 
26     public void setPid(Integer pid) {
27         this.pid = pid;
28     }
29 
30     public String getName() {
31         return this.name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37     public Idcard getIdcard() {
38         return idcard;
39     }
40 
41     public void setIdcard(Idcard idcard) {
42         this.idcard = idcard;
43     }
44 }

idcard.java类

 1 public class Idcard implements java.io.Serializable {
 2 
 3     // Fields
 4 
 5     private Integer iid;
 6     private String idcardno;
 7     private Person person;
 8 
 9     // Constructors
10 
11     /** default constructor */
12     public Idcard() {
13     }
14 
15     /** full constructor */
16     public Idcard(String idcardno) {
17         this.idcardno = idcardno;
18     }
19 
20     // Property accessors
21 
22     public Integer getIid() {
23         return this.iid;
24     }
25 
26     public void setIid(Integer iid) {
27         this.iid = iid;
28     }
29 
30     public String getIdcardno() {
31         return this.idcardno;
32     }
33 
34     public void setIdcardno(String idcardno) {
35         this.idcardno = idcardno;
36     }
37 
38     public Person getPerson() {
39         return person;
40     }
41 
42     public void setPerson(Person person) {
43         this.person = person;
44     }
45 
46 }

Person.hbm.xml

 1 <hibernate-mapping>
 2     <class name="com.db.entity.Person" table="person" catalog="mydb">
 3         <id name="pid" type="java.lang.Integer">
 4             <column name="pid" />
 5             <generator class="native" />
 6         </id>
 7         <property name="name" type="java.lang.String">
 8             <column name="name" length="30" />
 9         </property>
10         <one-to-one name="idcard" class="com.db.entity.Idcard" property-ref="person"></one-to-one>
11     </class>
12 </hibernate-mapping>

Idcard.hbm.xml

 1 <hibernate-mapping>
 2     <class name="com.db.entity.Idcard" table="idcard" catalog="mydb">
 3         <id name="iid" type="java.lang.Integer">
 4             <generator class="native">
 5             </generator>
 6         </id>
 7         <many-to-one name="person" column="pid" class="com.db.entity.Person"
 8             cascade="save-update" unique="true"></many-to-one>
 9         <property name="idcardno" type="java.lang.String">
10             <column name="idcardno" length="20" />
11         </property>
12     </class>
13 </hibernate-mapping>

bibernate.cfg.xml文件和测试代码同上。

 如果使用注解映射外键,则注解如下:

Idcard.java类

 1 public class Idcard implements java.io.Serializable {
 2 
 3     // Fields
 4     private Person person;
 5 
 6     @OneToOne
 7     @JoinColumn(name="pid")
 8     public Person getPerson() {
 9         return person;
10     }
11 
12     public void setPerson(Person person) {
13         this.person = person;
14     }
15 
16 }

Person.java类

 1 public class Person implements java.io.Serializable {
 2 
 3     // Fields
 4     private Idcard idcard;
 5 
 6     @OneToOne(mappedBy="person")
 7     public Idcard getIdcard() {
 8         return idcard;
 9     }
10 
11     public void setIdcard(Idcard idcard) {
12         this.idcard = idcard;
13     }
14 }

 

转载于:https://www.cnblogs.com/SaraMoring/p/5634044.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值