Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
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>)的更多相关文章
- hibernate中基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同
基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同,主要区别是在配置映射文件上会有区别 两个持久化类为Manager和Department 1:基于主键映射1-1关联关系 1)使用其他持久化 ...
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第15章处理并发问题-001事务并发问题及隔离机制介绍
1. 2.第一类丢失更新 3.脏读 4.虚读.幻读 5.不可重复读 6.第二类丢失更新 7.数据库的锁机制 8.数据库事务的隔离机制
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
随机推荐
- Jquery操作Cookie取值错误的解决方法
使用JQuery操作cookie时 发生取的值不正确,结果发现cookie有四个不同的属性,分享下错误的原因及解决方法. 使用JQuery操作cookie时 发生取的值不正确的问题: 结果发现coo ...
- How to running Job from a Form
For Example we wanna run a Job with name "FAN_TableList_CSV". So you must create a button ...
- hibernate4 使用及 新特性
hibernate4.x已经在官网出现一段时间了.下载地址: http://hibernate.org/orm/downloads/ 使用hibernate4所需要的jar包 在lib\require ...
- Microsoft server software support for Microsoft Azure virtual machines
http://support.microsoft.com/kb/2721672/en-us Article ID: 2721672 - Last Review: November 22, 2014 ...
- SQLserver中的xp_cmdshell
shell是用户与操作系统对话的一个接口,通过shell告诉操作系统让系统执行我们的指令 xp_cmdshell在sqlserver中默认是关闭的存在安全隐患. --打开xp_cmdshell ;;R ...
- iOS多线程编程Part 1/3 - NSThread & Run Loop
前言 多线程的价值无需赘述,对于App性能和用户体验都有着至关重要的意义,在iOS开发中,Apple提供了不同的技术支持多线程编程,除了跨平台的pthread之外,还提供了NSThread.NSOpe ...
- Another 20 Docs and Guides for Front-End Developers
http://www.sitepoint.com/another-20-docs-guides-front-end-developers/?utm_medium=email&utm_campa ...
- nodejs Q.js promise
var Q = require("q"); documentation for Qhttps://github.com/kriskowal/qhttps://github.com/ ...
- Memcache+Tomcat9集群实现session共享(非jar式配置, 手动编写Memcache客户端)
Windows上两个tomcat, 虚拟机中ip为192.168.0.30的centos上一个(测试用三台就够了, 为了测试看见端口所以没有使用nginx转发请求) 开始 1.windows上开启两个 ...
- 精灵的属性Zorder的设置
1.Zorder是CCSprite从父类CCNode那继承来的protected属性: class CCNode{ protected: int m_nZOrder; ...