1.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Monkey" table="MONKEYS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" column="NAME" type="string" /> <many-to-one name="homeAddress"
class="mypack.Address"
column="HOME_ADDRESS_ID"
cascade="all"
unique="true"
/> <many-to-one name="comAddress"
class="mypack.Address"
column="COM_ADDRESS_ID"
cascade="all"
unique="true"
/> </class> </hibernate-mapping>

2.

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Address" table="ADDRESSES" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="city" column="CITY" type="string" />
<property name="province" column="PROVINCE" type="string" />
<property name="street" column="STREET" type="string" />
<property name="zipcode" column="ZIPCODE" type="string" /> <one-to-one name="monkey"
class="mypack.Monkey"
property-ref="homeAddress"
/> </class>
</hibernate-mapping>

3.

 package mypack;

 public class Monkey {

     private Long id;
private String name;
private Address homeAddress;
private Address comAddress; public Monkey(String name, Address homeAddress, Address comAddress) {
this.name = name;
this.homeAddress = homeAddress;
this.comAddress = comAddress;
} /** default constructor */
public Monkey() {
} /** minimal constructor */
public Monkey(Address homeAddress, Address comAddress) {
this.homeAddress = homeAddress;
this.comAddress = comAddress;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public mypack.Address getHomeAddress() {
return this.homeAddress;
} public void setHomeAddress(mypack.Address homeAddress) {
this.homeAddress = homeAddress;
} public mypack.Address getComAddress() {
return this.comAddress;
} public void setComAddress(mypack.Address comAddress) {
this.comAddress = comAddress;
} }

4.

 package mypack;

 public class Address {
private Long id;
private String street;
private String city;
private String province;
private String zipcode;
private Monkey monkey; /** full constructor */
public Address(String province,String city,String street, String zipcode, Monkey monkey) {
this.street = street;
this.city = city;
this.province = province;
this.zipcode = zipcode;
this.monkey = monkey;
} /** default constructor */
public Address() {
} public String getStreet() {
return this.street;
} public void setStreet(String street) {
this.street = street;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getCity() {
return this.city;
} public void setCity(String city) {
this.city = city;
} public String getProvince() {
return this.province;
} public void setProvince(String province) {
this.province = province;
} public String getZipcode() {
return this.zipcode;
} public void setZipcode(String zipcode) {
this.zipcode = zipcode;
} public mypack.Monkey getMonkey() {
return this.monkey;
} public void setMonkey(mypack.Monkey monkey) {
this.monkey = monkey;
} }

5.

 package mypack;

 import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
} public void saveMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public Monkey loadMonkey(Long id){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Monkey monkey=(Monkey)session.get(Monkey.class,id);
Hibernate.initialize(monkey.getHomeAddress());
Hibernate.initialize(monkey.getComAddress());
tx.commit();
return monkey; }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void printMonkey(Monkey monkey) {
Address homeAddress=monkey.getHomeAddress();
Address comAddress=monkey.getComAddress();
System.out.println("Home Address of "+monkey.getName()+" is: "
+homeAddress.getProvince()+" "
+homeAddress.getCity()+" "
+homeAddress.getStreet()); System.out.println("Company Address of "+monkey.getName()+" is: "
+comAddress.getProvince()+" "
+comAddress.getCity()+" "
+comAddress.getStreet()); if(homeAddress.getMonkey()==null)
System.out.println("Can not naviagte from homeAddress to Monkey."); if(comAddress.getMonkey()==null)
System.out.println("Can not naviagte from comAddress to Monkey."); } public void test(){ Monkey monkey=new Monkey();
Address homeAddress=new Address("province1","city1","street1","100001",monkey);
Address comAddress=new Address("province2","city2","street2","200002",monkey);
monkey.setName("Tom");
monkey.setHomeAddress(homeAddress);
monkey.setComAddress(comAddress); saveMonkey(monkey);
monkey=loadMonkey(monkey.getId());
printMonkey(monkey); } public static void main(String args[]) {
new BusinessService().test();
sessionFactory.close();
}
}

6.

 drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB; create table MONKEYS (
ID bigint not null,
NAME varchar(15),
HOME_ADDRESS_ID bigint unique,
COM_ADDRESS_ID bigint unique,
primary key (ID)
); create table ADDRESSES(
ID bigint not null,
CITY varchar(128),
STREET varchar(128),
PROVINCE varchar(128),
ZIPCODE varchar(6),
primary key(ID)
); alter table MONKEYS add index IDX_HOME_ADDRESS(HOME_ADDRESS_ID),
add constraint FK_HOME_ADDRESS foreign key (HOME_ADDRESS_ID) references ADDRESSES(ID); alter table MONKEYS add index IDX_COM_ADDRESS(COM_ADDRESS_ID),
add constraint FK_COM_ADDRESS foreign key (COM_ADDRESS_ID) references ADDRESSES(ID);

7.

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

  1. hibernate中基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同

    基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同,主要区别是在配置映射文件上会有区别 两个持久化类为Manager和Department 1:基于主键映射1-1关联关系 1)使用其他持久化 ...

  2. Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  3. Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  4. Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  5. Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多

    0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...

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

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  7. Hibernate逍遥游记-第15章处理并发问题-001事务并发问题及隔离机制介绍

    1. 2.第一类丢失更新 3.脏读 4.虚读.幻读 5.不可重复读 6.第二类丢失更新 7.数据库的锁机制 8.数据库事务的隔离机制

  8. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  9. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

随机推荐

  1. php文本操作方法集合比较

    fgets和fputs.fread和fwrite.fscanf和fprintf 字符串读写函数fgets和fputs 一.读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符数组中,函数 ...

  2. Oracle 表的连接方式(2)-----HASH JOIN的基本机制3

    HASH JOIN的模式 hash join有三种工作模式,分别是optimal模式,onepass模式和multipass模式,分别在v$sysstat里面有对应的统计信息: SQL> sel ...

  3. tomcat中server.xml文件解析

    下面我们将讲述这个文件中的基本配置信息,更具体的配置信息见tomcat的文档 元素名 属性 解释 server port 指定一个端口,这个端口负责监听关闭tomcat的请求 shutdown 指定向 ...

  4. Java中的继承和多态

    1.  什么是继承,继承的特点? 子类继承父类的特征和行为,使得子类具有父类的各种属性和方法.或子类从父类继承方法,使得子类具有父类相同的行为. 特点:在继承关系中,父类更通用.子类更具体.父类具有更 ...

  5. Inject js code to exchange 2013

    1. save the following code to C:\Program Files\Microsoft\Exchange Server\V15\FrontEnd\HttpProxy\owa ...

  6. android 中怎么控制checkbox中文本与左侧box的距离

    使用paddingLeft属性可以控制宽度.默认比较宽 效果如图:

  7. Eclipse配置默认的编码集为utf-8

    既然开了博,那就来点有用的. 可以使用下面的方法,让Eclipse对所有的项目里所有文件都按照指定的编码解析. Eclipse安装目录下有一个eclipse.ini文件, 用记事本打开即可,在最后一行 ...

  8. 剑指offer--面试题12

    题目:打印从1~最大的n位数 分析:知道陷阱在哪,即n很大时若用通常的int,long会溢出:想到用字符串解决,这涉及到字符转数字及反过来. 刚开始纠结于字符串怎么加1,想了片刻,觉得应该取出最后一位 ...

  9. NOI 国家集训队论文集

    鉴于大家都在找这些神牛的论文.我就转载了这篇论文合集 国家集训队论文分类 组合数学 计数与统计 2001 - 符文杰:<Pólya原理及其应用> 2003 - 许智磊:<浅谈补集转化 ...

  10. 暑假集训单切赛第一场 CF 191A Dynasty Puzzles

    题意不说了,看原题吧,思路见代码: #include <iostream> #include <stdio.h> #include <string.h> #incl ...