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">)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. Hibernate逍遥游记-第15章处理并发问题-002悲观锁

    1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...

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

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

  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. 免费GIT托管

    http://www.gitcentral.com http://www.projectlocker.com http://gitfarm.appspot.com http://code.google ...

  2. [大牛翻译系列]Hadoop(15)MapReduce 性能调优:优化MapReduce的用户JAVA代码

    6.4.5 优化MapReduce用户JAVA代码 MapReduce执行代码的方式和普通JAVA应用不同.这是由于MapReduce框架为了能够高效地处理海量数据,需要成百万次调用map和reduc ...

  3. Android SDK 更新失败

    万恶的墙,调查兵团赶紧把墙拆了.大家一起跟巨人打一架. 解决方法是改hosts文件 添加 74.125.237.1 dl-ssl.google.com ok,good job 多亏了http://bl ...

  4. 动态创建MySQL数据库

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

  5. 加载页面遮挡耗时操作任务页面--第三方开源--AndroidProgressLayout

    在Android的开发中,往往有这种需求,比如一个耗时的操作,联网获取网络图片.内容,数据库耗时读写等等,在此耗时操作过程中,开发者也许不希望用户再进行其他操作(其他操作可能会引起逻辑混乱),而此时需 ...

  6. ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

    测试库一条update语句报错:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> ...

  7. 升级 CentOS git 1.7.1 到 1.7.12

    CentOS 源里的 git 版本是 1.7.1,如果远程创建的库所用 git 的版本比它高,在 pull 的时候,如果本地有修改,就会永久阻塞:在 push 的时候就会失败. CentOS 源里的 ...

  8. 游刃于MVC、WCF中的Autofac

    为了程序的健壮性.扩展性.可维护性,依赖抽象而不是具体实现类等等,于是我选择了Autofac依赖注入容器 就是这个工厂来降低耦合.之前买东西是自己去超市,现在呢 我需要什么东西,他们给送过来直接拿到了 ...

  9. Configuring My Site in SharePoint 2010

    Configuring the User Profile Service in SharePoint 2010 http://sharepointgeorge.com/2010/configuring ...

  10. div+css布局细节问题

    cursor: pointer;在chrome里支持,hand不支持