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. IIS6下, web.config配置为targetFramework="4.0"时出404错误

    打开IIS管理器,在"Web 服务扩展" 中 将ASP.NET v4.0设置为允许就好了.这个选项默认是禁止的.

  2. python自学笔记二

    :#进入循环重输文0件名 pass else:#退出循环,等待创建 break fobj = open(fname,'a')#打开或创建文件 #接下来写入文件 all = [] print('ente ...

  3. 【译】Android系统简介

    简介  本文主要介绍Android的基础知识和体系结构,本文主题: 简介什么是Android,为什么开发者需要关注Android: Android体系结构(如Linux Kernel, Librari ...

  4. Configure Log Shipping

    准备工作 两台装有的Windows Server 2012R2以及SQL Server 2012的服务器 下载评估版 Windows Server 2012 R2 下载 Microsoft SQL S ...

  5. <亲测好使>mac os 安装mcrypt扩展

    以前安装opencart的时候倒是不需要mcrypt 这个库.但是新版本需要了.加上自己的 是mac环境.当时闲麻烦,就一直没装.这次下午就寻思给装上吧! 1.首先你要先安装xcode这个工具.不然没 ...

  6. ASP.NET 运行机制

    原本今天打算继续写ASP.NET MVC第四天的.但是由于里面涉及到asp.net运行机制的原理,如果不分析一下这里,mvc想说清楚还是挺困难的.既然要提到asp.net运行机制,所以打算还是说详细一 ...

  7. IE8的Textarea滚动条乱跳的解决方案

    最近在弄的一个项目,其中一个页面需要输入很长的文字,因为文字是纯文本的,所以用了Textarea,在webkit下没有任何问题,结果在IE8下测试时,发现当文本超超出Textarea的大小时,在输入文 ...

  8. VBS基础篇 - 循环

    经常地,当编写代码时,我们希望将一段代码执行若干次,我们可以在代码中使用循环语句来完成这项工作. 循环可分为三类:一类在条件变为 False 之前重复执行语句,一类在条件变为 True 之前重复执行语 ...

  9. input框中value与placeholder的区别

    value:是input中预先放置的文字,当鼠标点击之后依然存在,是值的一部分. placeholder:是input中输入位置的默认显示的文字,鼠标点击后仍旧显示,但不属于值,类似于背景.

  10. [原创汉化]linux前端神器 WebStorm8 汉化

    只汉化了linux版本 因为linux的工具没win多 不过汉化应该都通用的,自行尝试下. 汉化的不是很完全.有时间放出完全版本来.汉化是个体力活 转载随易,汉化不易,且转且注明 截图: http:/ ...