Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)

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     <one-to-one name="address" 
15         class="mypack.Address"
16         cascade="all" 
17      />
18         
19   </class>
20 
21 </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="foreign">
10         <param name="property">monkey</param>
11       </generator>
12     </id>
13 
14     <property name="city" column="CITY" type="string" />
15     <property name="province" column="PROVINCE" type="string" />
16     <property name="street" column="STREET" type="string" />
17     <property name="zipcode" column="ZIPCODE" type="string" />
18         
19     <one-to-one name="monkey" 
20         class="mypack.Monkey"
21        constrained="true"
22     />
23 
24   </class>
25 </hibernate-mapping>

3.

 1 package mypack;
 2 public class Monkey {
 3 
 4     private Long id;
 5     private String name;
 6     private Address address;
 7 
 8     public Monkey(String name, Address address) {
 9         this.name = name;
10         this.address = address;
11      }
12 
13     /** default constructor */
14     public Monkey() {
15     }
16 
17     /** minimal constructor */
18     public Monkey(Address address) {
19         this.address = address;
20     }
21 
22     public Long getId() {
23         return this.id;
24     }
25 
26     public void setId(Long 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 
38     public mypack.Address getAddress() {
39         return this.address;
40     }
41 
42     public void setAddress(mypack.Address address) {
43         this.address = address;
44     }
45 
46 }

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  
36 
37 
38  public Monkey loadMonkey(Long id){
39     Session session = sessionFactory.openSession();
40     Transaction tx = null;
41     try {
42       tx = session.beginTransaction();
43       Monkey monkey=(Monkey)session.get(Monkey.class,id);
44       tx.commit();
45       
46       return monkey;
47 
48     }catch (RuntimeException e) {
49       if (tx != null) {
50         tx.rollback();
51       }
52       throw e;
53     } finally {
54        session.close();
55     }
56   }
57    
58   public void printMonkey(Monkey monkey){
59     Address address=monkey.getAddress();
60     System.out.println("Address of "+monkey.getName()+" is: " 
61       +address.getProvince()+" "
62       +address.getCity()+" "
63       +address.getStreet());
64 
65     if(address.getMonkey()==null)
66       System.out.println("Can not naviagte from address to Monkey.");
67    
68   }
69 
70  public void test(){
71      
72       Monkey monkey=new Monkey();
73       Address address=new Address("province1","city1","street1","100001",monkey);
74       monkey.setName("Tom");
75       monkey.setAddress(address);
76      
77       saveMonkey(monkey);
78       monkey=loadMonkey(monkey.getId());
79       printMonkey(monkey);
80       
81   }
82 
83   public static void main(String args[]){
84     new BusinessService().test();
85     sessionFactory.close();
86   }
87 }

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    primary key (ID)
 9 );
10 
11 create table ADDRESSES(
12    ID bigint not null,
13    STREET varchar(128),
14    CITY varchar(128),
15    PROVINCE varchar(128),
16    ZIPCODE varchar(6),
17    primary key (ID)
18 );
19 
20 alter table ADDRESSES add index IDX_MONKEY(ID), add constraint FK_MONKEY foreign key (ID) references MONKEYS(ID);

7.

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