Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
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" /> <one-to-one name="address"
class="mypack.Address"
cascade="all"
/> </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="foreign">
<param name="property">monkey</param>
</generator>
</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"
constrained="true"
/> </class>
</hibernate-mapping>

3.
package mypack;
public class Monkey { private Long id;
private String name;
private Address address; public Monkey(String name, Address address) {
this.name = name;
this.address = address;
} /** default constructor */
public Monkey() {
} /** minimal constructor */
public Monkey(Address address) {
this.address = address;
} 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 getAddress() {
return this.address;
} public void setAddress(mypack.Address address) {
this.address = address;
} }
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);
tx.commit(); return monkey; }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void printMonkey(Monkey monkey){
Address address=monkey.getAddress();
System.out.println("Address of "+monkey.getName()+" is: "
+address.getProvince()+" "
+address.getCity()+" "
+address.getStreet()); if(address.getMonkey()==null)
System.out.println("Can not naviagte from address to Monkey."); } public void test(){ Monkey monkey=new Monkey();
Address address=new Address("province1","city1","street1","100001",monkey);
monkey.setName("Tom");
monkey.setAddress(address); 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),
primary key (ID)
); create table ADDRESSES(
ID bigint not null,
STREET varchar(128),
CITY varchar(128),
PROVINCE varchar(128),
ZIPCODE varchar(6),
primary key (ID)
); alter table ADDRESSES add index IDX_MONKEY(ID), add constraint FK_MONKEY foreign key (ID) references MONKEYS(ID);
7.
Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)的更多相关文章
- 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章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第15章处理并发问题-002悲观锁
1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...
- hibernate中基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同
基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同,主要区别是在配置映射文件上会有区别 两个持久化类为Manager和Department 1:基于主键映射1-1关联关系 1)使用其他持久化 ...
- 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 ...
随机推荐
- 2013-07-24 IT 要闻速记快想
### ========================= ###凡客有闹钟?从凡客的角度来讲,闹钟等工具类应用是为推广品牌和产品服务,通过工具类产品给大众一个对凡客品牌的认知.而选择推出工具类的产品 ...
- HTML表单样式
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- [转]null和""以及==与equals的区别
String str1 = null; str引用为空 String str2 = ""; str引用为空串 直接点就是null没有分配内存空间,而""分配了内 ...
- Go在linux下的安装
在Ubuntu.Debian 或者 Linux Mint上安装Go语言 下面是在基于Debian的发行版上使用apt-get来安装Go语言和它的开发工具. $ sudo apt-get install ...
- JDBC连接数据库代码
//连接是需要导包 http://pan.baidu.com/s/1o6nyuOa /*配合数据库建立表 create database day14 character set utf8 collat ...
- sql replace
update dbo.EquipmentAttribute set AttributeName=replace(AttributeName,' ','') where EquipmentID=8 ...
- linux-CentOS6.4下安装oracle11g详解
参考地址:http://dengqsintyt.iteye.com/blog/1991930
- easyui的datagrid和panel如何让标题动态改变?
解决方法: 用$(this).datagrid("getPanel").panel("setTitle","new title").$(th ...
- bnuoj 33647 Angry Grammar Nazi(字符串)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=33647 [题意]:字符串匹配,暴力配就行了 [题解]:截出单词,然后进行匹配就行了 [code]: ...
- dbutils 执行sql返回的数据类型
//ArrayHandler: 把结果集中的第一行数据转成对象数组 //ArrayListHandler:把结果集中的每一行数据都转成一个对象数组,再存放到List中 //BeanHandler:将结 ...