Hibernate一对一关联------外键关联(亲测成功)

1、创建实体(Address.java、Address1.java、Client.java)

 1 package wck.stu.vo.onetooneout;
 2 
 3 public class Address {
 4     private String id = ""; //主键
 5     
 6     private String province = "";//省份
 7     
 8     private String city = "";//城市
 9     
10     private String shreet = "";//街道
11     
12     private String zipcode = "";//邮编
13     
14     private Client client;//关联另外一个类
15 
16     public String getId() {
17         return id;
18     }
19 
20     public void setId(String id) {
21         this.id = id;
22     }
23 
24     public String getProvince() {
25         return province;
26     }
27 
28     public void setProvince(String province) {
29         this.province = province;
30     }
31 
32     public String getCity() {
33         return city;
34     }
35 
36     public void setCity(String city) {
37         this.city = city;
38     }
39 
40     public String getShreet() {
41         return shreet;
42     }
43 
44     public void setShreet(String shreet) {
45         this.shreet = shreet;
46     }
47 
48     public String getZipcode() {
49         return zipcode;
50     }
51 
52     public void setZipcode(String zipcode) {
53         this.zipcode = zipcode;
54     }
55 
56     public Client getClient() {
57         return client;
58     }
59 
60     public void setClient(Client client) {
61         this.client = client;
62     }
63     
64     
65 }
Address.java
 1 package wck.stu.vo.onetooneout;
 2 
 3 public class Address {
 4     private String id = ""; //主键
 5     
 6     private String province = "";//省份
 7     
 8     private String city = "";//城市
 9     
10     private String shreet = "";//街道
11     
12     private String zipcode = "";//邮编
13     
14     private Client client;//关联另外一个类
15 
16     public String getId() {
17         return id;
18     }
19 
20     public void setId(String id) {
21         this.id = id;
22     }
23 
24     public String getProvince() {
25         return province;
26     }
27 
28     public void setProvince(String province) {
29         this.province = province;
30     }
31 
32     public String getCity() {
33         return city;
34     }
35 
36     public void setCity(String city) {
37         this.city = city;
38     }
39 
40     public String getShreet() {
41         return shreet;
42     }
43 
44     public void setShreet(String shreet) {
45         this.shreet = shreet;
46     }
47 
48     public String getZipcode() {
49         return zipcode;
50     }
51 
52     public void setZipcode(String zipcode) {
53         this.zipcode = zipcode;
54     }
55 
56     public Client getClient() {
57         return client;
58     }
59 
60     public void setClient(Client client) {
61         this.client = client;
62     }
63     
64     
65 }
Address1.java
 1 package wck.stu.vo.onetooneout;
 2 
 3 public class Client {
 4     private String id = "";//主键
 5     
 6     private String clietName = "";//客户名称
 7     
 8     private String phone = "";//客户电话
 9     
10     private String email = "";//客户邮箱
11     
12     private Address address;//关联另外一个PO
13     
14     private Address1 address1;
15 
16     public String getId() {
17         return id;
18     }
19 
20     public void setId(String id) {
21         this.id = id;
22     }
23 
24     public String getClietName() {
25         return clietName;
26     }
27 
28     public void setClietName(String clietName) {
29         this.clietName = clietName;
30     }
31 
32     public String getPhone() {
33         return phone;
34     }
35 
36     public void setPhone(String phone) {
37         this.phone = phone;
38     }

39 
40     public String getEmail() {
41         return email;
42     }
43 
44     public void setEmail(String email) {
45         this.email = email;
46     }
47 
48     public Address getAddress() {
49         return address;
50     }
51 
52     public void setAddress(Address address) {
53         this.address = address;
54     }
55 
56     public Address1 getAddress1() {
57         return address1;
58     }
59 
60     public void setAddress1(Address1 address1) {
61         this.address1 = address1;
62     }
63 }
Client.java


2、配置文件(Address.hbm.xml、Address1.hbm.xml、Client.hbm.xml)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping>
 6     <class name="wck.stu.vo.onetooneout.Address" table="ADDRESS">
 7         <id name="id" type="java.lang.String">
 8             <column name="C_ID" length="32" not-null="true"></column>
 9             <generator class="uuid.hex"></generator>
10         </id>
11         <property name="province" type="java.lang.String">
12             <column name="C_PROVINCE" length="17"></column>
13         </property>
14         <property name="city" type="java.lang.String">
15             <column name="C_CITY" length="17"></column>
16         </property>
17         <property name="shreet" type="java.lang.String">
18             <column name="C_SHREET" length="17"></column>
19         </property>
20         <property name="zipcode" type="java.lang.String">
21             <column name="C_ZIPCODE" length="170"></column>
22         </property>
23          <one-to-one name="client" class="wck.stu.vo.onetooneout.Client" property-ref="address"/>
24     </class>
25 </hibernate-mapping>
Address.hbm.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping>
 6     <class name="wck.stu.vo.onetooneout.Address1" table="ADDRESS1">
 7         <id name="id" type="java.lang.String">
 8             <column name="C_ID" length="32" not-null="true"></column>
 9             <generator class="uuid.hex"></generator>
10         </id>
11         <property name="province" type="java.lang.String">
12             <column name="C_PROVINCE" length="17"></column>
13         </property>
14         <property name="city" type="java.lang.String">
15             <column name="C_CITY" length="17"></column>
16         </property>
17         <property name="shreet" type="java.lang.String">
18             <column name="C_SHREET" length="17"></column>
19         </property>
20         <property name="zipcode" type="java.lang.String">
21             <column name="C_ZIPCODE" length="170"></column>
22         </property>
23          <one-to-one name="client" class="wck.stu.vo.onetooneout.Client" property-ref="address"/>
24     </class>
25 </hibernate-mapping>
Address1.hbm.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping>
 6     <class name="wck.stu.vo.onetooneout.Client" table="CLIENT">
 7         <id name="id" type="java.lang.String">
 8             <column name="C_ID" length="32" not-null="true"></column>
 9             <generator class="uuid.hex"></generator>
10         </id>
11         <property name="clietName" type="java.lang.String">
12             <column name="C_CLIENTNAME" length="17"></column>
13         </property>
14         <property name="phone" type="java.lang.String">
15             <column name="C_PHONE" length="17"></column>
16         </property>
17         <property name="email" type="java.lang.String">
18             <column name="C_EMAIL" length="170"></column>
19         </property>
20         <many-to-one name="address" class="wck.stu.vo.onetooneout.Address" column="address" cascade="all" lazy="false" unique="true"/>
21         <many-to-one name="address1" class="wck.stu.vo.onetooneout.Address1" column="address1" cascade="all" lazy="false" unique="true"/>
22     </class>
23 </hibernate-mapping>
Client.hbm.xml


3、测试业务逻辑层代码

 1 public String saveClientInfo() {
 2         Client client = new Client();
 3         Address address = new Address();
 4         address.setProvince("北京市");
 5         address.setCity("北京市");
 6         address.setShreet("清华园");
 7         address.setZipcode("100084");
 8         Address1 address1 = new Address1();
 9         address1.setProvince("北京市");
10         address1.setCity("北京市");
11         address1.setShreet("清华园");
12         address1.setZipcode("100084");
13         client.setClietName("李想");
14         client.setPhone("010-56565566");
15         client.setEmail("[email protected]");
16         //PO对象之间相互设置关联关系
17         address1.setClient(client);
18         address.setClient(client);
19         client.setAddress(address);
20         client.setAddress1(address1);
21         onettooneDaoImpl.saveClienInfo(client);
22         return client.getId();
23     }

 

你可能感兴趣的:(Hibernate一对一关联------外键关联(亲测成功))