Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)

1.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7   <class name="mypack.Monkey" table="MONKEYS" >
 8     <id name="id" type="long" column="ID">
 9       <generator class="increment"/>
10     </id>
11 
12     <property name="name" column="NAME" type="string" />
13        
14     <many-to-one name="homeAddress" 
15         class="mypack.Address"
16         column="HOME_ADDRESS_ID"
17         cascade="all" 
18         unique="true"
19     />
20 
21     <many-to-one name="comAddress" 
22         class="mypack.Address"
23         column="COM_ADDRESS_ID"
24         cascade="all"
25         unique="true"
26     />
27         
28   </class>
29 
30 </hibernate-mapping>

2.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7   <class name="mypack.Address" table="ADDRESSES" >
 8     <id name="id" type="long" column="ID">
 9       <generator class="increment"/>
10     </id>
11 
12     <property name="city" column="CITY" type="string" />
13     <property name="province" column="PROVINCE" type="string" />
14     <property name="street" column="STREET" type="string" />
15     <property name="zipcode" column="ZIPCODE" type="string" />
16         
17     <one-to-one name="monkey" 
18         class="mypack.Monkey"
19        property-ref="homeAddress"
20     />
21 
22   </class>
23 </hibernate-mapping>

3.

 1 package mypack;
 2 
 3 public class Monkey {
 4 
 5 
 6     private Long id;
 7     private String name;
 8     private Address homeAddress;
 9     private Address comAddress;
10 
11     public Monkey(String name, Address homeAddress, Address comAddress) {
12         this.name = name;
13         this.homeAddress = homeAddress;
14         this.comAddress = comAddress;
15     }
16 
17     /** default constructor */
18     public Monkey() {
19     }
20 
21     /** minimal constructor */
22     public Monkey(Address homeAddress, Address comAddress) {
23         this.homeAddress = homeAddress;
24         this.comAddress = comAddress;
25     }
26 
27     public Long getId() {
28         return this.id;
29     }
30 
31     public void setId(Long id) {
32         this.id = id;
33     }
34 
35     public String getName() {
36         return this.name;
37     }
38 
39     public void setName(String name) {
40         this.name = name;
41     }
42 
43     public mypack.Address getHomeAddress() {
44         return this.homeAddress;
45     }
46 
47     public void setHomeAddress(mypack.Address homeAddress) {
48         this.homeAddress = homeAddress;
49     }
50 
51     public mypack.Address getComAddress() {
52         return this.comAddress;
53     }
54 
55     public void setComAddress(mypack.Address comAddress) {
56         this.comAddress = comAddress;
57     }
58 
59 }

4.

 1 package mypack;
 2 
 3 public class Address {
 4     private Long id;
 5     private String street;
 6     private String city;
 7     private String province;
 8     private String zipcode;
 9     private Monkey monkey;
10 
11     /** full constructor */
12     public Address(String province,String city,String street, String zipcode, Monkey monkey) {
13         this.street = street;
14         this.city = city;
15         this.province = province;
16         this.zipcode = zipcode;
17         this.monkey = monkey;
18     }
19 
20     /** default constructor */
21     public Address() {
22     }
23 
24     public String getStreet() {
25         return this.street;
26     }
27 
28     public void setStreet(String street) {
29         this.street = street;
30     }
31 
32     public Long getId() {
33         return this.id;
34     }
35 
36     public void setId(Long id) {
37         this.id = id;
38     }
39 
40     public String getCity() {
41         return this.city;
42     }
43 
44     public void setCity(String city) {
45         this.city = city;
46     }
47 
48     public String getProvince() {
49         return this.province;
50     }
51 
52     public void setProvince(String province) {
53         this.province = province;
54     }
55 
56     public String getZipcode() {
57         return this.zipcode;
58     }
59 
60     public void setZipcode(String zipcode) {
61         this.zipcode = zipcode;
62     }
63 
64     public mypack.Monkey getMonkey() {
65         return this.monkey;
66     }
67 
68     public void setMonkey(mypack.Monkey monkey) {
69         this.monkey = monkey;
70     }
71 
72 }

5.

 1 package mypack;
 2 
 3 import org.hibernate.*;
 4 import org.hibernate.cfg.Configuration;
 5 import java.util.*;
 6 
 7 public class BusinessService{
 8   public static SessionFactory sessionFactory;
 9   static{
10      try{
11        Configuration config = new Configuration().configure();
12        sessionFactory = config.buildSessionFactory();
13     }catch(RuntimeException e){e.printStackTrace();throw e;}
14   }
15 
16   
17   public void saveMonkey(Monkey monkey){
18     Session session = sessionFactory.openSession();
19     Transaction tx = null;
20     try {
21       tx = session.beginTransaction();
22       session.save(monkey);
23       tx.commit();
24 
25     }catch (RuntimeException e) {
26       if (tx != null) {
27         tx.rollback();
28       }
29       throw e;
30     } finally {
31       session.close();
32     }
33   }
34 
35  public Monkey loadMonkey(Long id){
36     Session session = sessionFactory.openSession();
37     Transaction tx = null;
38     try {
39       tx = session.beginTransaction();
40       Monkey monkey=(Monkey)session.get(Monkey.class,id);
41       Hibernate.initialize(monkey.getHomeAddress());
42       Hibernate.initialize(monkey.getComAddress());
43       tx.commit();
44       return monkey;
45 
46     }catch (RuntimeException e) {
47       if (tx != null) {
48         tx.rollback();
49       }
50       throw e;
51     } finally {
52       session.close();
53     }
54   }
55    
56   public void printMonkey(Monkey monkey) {
57     Address homeAddress=monkey.getHomeAddress();
58     Address comAddress=monkey.getComAddress();
59     System.out.println("Home Address of "+monkey.getName()+" is: " 
60       +homeAddress.getProvince()+" "
61       +homeAddress.getCity()+" "
62       +homeAddress.getStreet());
63 
64     System.out.println("Company Address of "+monkey.getName()+" is: "
65       +comAddress.getProvince()+" "
66       +comAddress.getCity()+" "
67       +comAddress.getStreet());
68 
69     if(homeAddress.getMonkey()==null)
70       System.out.println("Can not naviagte from homeAddress to Monkey.");
71 
72     if(comAddress.getMonkey()==null)
73       System.out.println("Can not naviagte from comAddress to Monkey.");
74     
75   }
76 
77  public void test(){
78      
79       Monkey monkey=new Monkey();
80       Address homeAddress=new Address("province1","city1","street1","100001",monkey);
81       Address comAddress=new Address("province2","city2","street2","200002",monkey);
82       monkey.setName("Tom");
83       monkey.setHomeAddress(homeAddress);
84       monkey.setComAddress(comAddress);
85 
86       saveMonkey(monkey);
87       monkey=loadMonkey(monkey.getId());
88       printMonkey(monkey);
89 
90       
91   }
92 
93   public static void main(String args[]) {
94     new BusinessService().test();
95     sessionFactory.close();
96   }
97 }

6.

 1 drop database if exists SAMPLEDB;
 2 create database SAMPLEDB;
 3 use SAMPLEDB;
 4 
 5 create table MONKEYS (
 6    ID bigint not null,
 7    NAME varchar(15),
 8    HOME_ADDRESS_ID bigint unique,
 9    COM_ADDRESS_ID bigint unique,
10    primary key (ID)
11 );
12 
13 create table ADDRESSES(
14   ID bigint not null,
15   CITY varchar(128),
16   STREET varchar(128),
17   PROVINCE varchar(128),
18   ZIPCODE varchar(6),
19   primary key(ID)
20 );
21 
22 alter table MONKEYS add index IDX_HOME_ADDRESS(HOME_ADDRESS_ID), 
23 add constraint FK_HOME_ADDRESS foreign key (HOME_ADDRESS_ID) references ADDRESSES(ID);
24 
25 alter table MONKEYS add index IDX_COM_ADDRESS(COM_ADDRESS_ID), 
26 add constraint FK_COM_ADDRESS foreign key (COM_ADDRESS_ID) references ADDRESSES(ID);

7.

原文地址:https://www.cnblogs.com/shamgod/p/5299718.html