一、

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" type="string" column="NAME" /> <component name="homeAddress" class="mypack.Address">
<parent name="monkey" />
<property name="province" type="string" column="HOME_PROVINCE"/>
<property name="city" type="string" column="HOME_CITY"/>
<property name="street" type="string" column="HOME_STREET"/>
<property name="zipcode" type="string" column="HOME_ZIPCODE"/>
</component> <component name="comAddress" class="mypack.Address">
<parent name="monkey" />
<property name="province" type="string" column="COM_PROVINCE"/>
<property name="city" type="string" column="COM_CITY"/>
<property name="street" type="string" column="COM_STREET"/>
<property name="zipcode" type="string" column="COM_ZIPCODE"/>
</component>
</class> </hibernate-mapping>

2.

 package mypack;
public class Monkey { private long id;
private String name;
private Address homeAddress;
private Address comAddress; public Monkey() {
} public Monkey(String name, Address homeAddress, Address comAddress) {
this.name = name;
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 Address getHomeAddress() {
return this.homeAddress;
} public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Address getComAddress() {
return this.comAddress;
} public void setComAddress(Address comAddress) {
this.comAddress = comAddress;
} }

3.

 package mypack;
public class Address { private String province;
private String city;
private String street;
private String zipcode;
private Monkey monkey; public Address() {
} public Address(String province, String city, String street, String zipcode,Monkey monkey) {
this.province = province;
this.city = city;
this.street = street;
this.zipcode = zipcode;
this.monkey=monkey;
} public String getProvince() {
return this.province;
} public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return this.city;
} public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return this.street;
} public void setStreet(String street) {
this.street = street;
}
public String getZipcode() {
return this.zipcode;
} public void setZipcode(String zipcode) {
this.zipcode = zipcode;
} public Monkey getMonkey() {
return this.monkey;
} public void setMonkey(Monkey monkey) {
this.monkey = monkey;
} }

二、电脑例子

4.

 <?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.Computer" table="COMPUTERS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="type" type="string" >
<column name="COMPUTER_TYPE" />
</property> <component name="cpuBox" class="mypack.CpuBox">
<parent name="computer" /> <property name="type" type="string" >
<column name="CPUBOX_TYPE" />
</property> <component name="graphicsCard" class="mypack.GraphicsCard">
<parent name="cpuBox" /> <property name="type" type="string" >
<column name="GRAPHICSCARD_TYPE" />
</property> </component> <many-to-one
name="vendor"
column="CPUBOX_VENDOR_ID"
class="mypack.Vendor"
not-null="true"
/>
</component>
</class> </hibernate-mapping>

5.

 <?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.Vendor" table="VENDORS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="type" type="string" >
<column name="TYPE" length="15" />
</property> </class> </hibernate-mapping>

6.

 package mypack;
public class Computer { private long id;
private String type;
private CpuBox cpuBox; public Computer() {
} public Computer(CpuBox cpuBox) {
this.cpuBox = cpuBox;
}
public Computer(String type, CpuBox cpuBox) {
this.type = type;
this.cpuBox = cpuBox;
} public long getId() {
return this.id;
} public void setId(long id) {
this.id = id;
}
public String getType() {
return this.type;
} public void setType(String type) {
this.type = type;
}
public CpuBox getCpuBox() {
return this.cpuBox;
} public void setCpuBox(CpuBox cpuBox) {
this.cpuBox = cpuBox;
} }

7.

 package mypack;
public class CpuBox { private String type;
private GraphicsCard graphicsCard;
private Vendor vendor;
private Computer computer;
public CpuBox() {
} public CpuBox(Vendor vendor) {
this.vendor = vendor;
}
public CpuBox(String type, GraphicsCard graphicsCard, Vendor vendor,Computer computer) {
this.type = type;
this.graphicsCard = graphicsCard;
this.vendor = vendor;
this.computer=computer;
} public String getType() {
return this.type;
} public void setType(String type) {
this.type = type;
}
public GraphicsCard getGraphicsCard() {
return this.graphicsCard;
} public void setGraphicsCard(GraphicsCard graphicsCard) {
this.graphicsCard = graphicsCard;
}
public Vendor getVendor() {
return this.vendor;
} public void setVendor(Vendor vendor) {
this.vendor = vendor;
} public Computer getComputer() {
return this.computer;
} public void setComputer(Computer computer) {
this.computer=computer;
} }

8.

 package mypack;

 public class GraphicsCard  {

      private String type;
private CpuBox cpuBox; public GraphicsCard() {
} public GraphicsCard(String type,CpuBox cpuBox) {
this.type = type;
this.cpuBox=cpuBox;
} public String getType() {
return this.type;
} public void setType(String type) {
this.type = type;
} public CpuBox getCpuBox() {
return this.cpuBox;
} public void setCpuBox(CpuBox cpuBox) {
this.cpuBox = cpuBox;
} }

9.

 package mypack;
public class Vendor { private long id;
private String type; public Vendor() {
} public Vendor(String type) {
this.type = type;
} public long getId() {
return this.id;
} public void setId(long id) {
this.id = id;
}
public String getType() {
return this.type;
} public void setType(String type) {
this.type = type;
} }

10.

 use sampledb;
drop table if exists COMPUTERS;
drop table if exists MONKEYS;
drop table if exists VENDORS; create table MONKEYS (ID bigint not null, NAME varchar(15), HOME_PROVINCE varchar(255),
HOME_CITY varchar(255), HOME_STREET varchar(255), HOME_ZIPCODE varchar(255),
COM_PROVINCE varchar(255), COM_CITY varchar(255),
COM_STREET varchar(255), COM_ZIPCODE varchar(255), primary key (ID)); create table COMPUTERS (ID bigint not null, COMPUTER_TYPE varchar(15),
CPUBOX_TYPE varchar(15), GRAPHICSCARD_TYPE varchar(15),
CPUBOX_VENDOR_ID bigint not null, primary key (ID)); create table VENDORS (ID bigint not null, TYPE varchar(15), primary key (ID)); alter table COMPUTERS add index IDX_VENDOR (CPUBOX_VENDOR_ID),
add constraint FK_VENDOR foreign key (CPUBOX_VENDOR_ID)
references VENDORS (ID);

11.

 <?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sampledb
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
1234
</property> <property name="show_sql">true</property> <mapping resource="mypack/Monkey.hbm.xml" />
<mapping resource="mypack/Computer.hbm.xml" />
<mapping resource="mypack/Vendor.hbm.xml" /> </session-factory>
</hibernate-configuration>

Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)的更多相关文章

  1. Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)

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

  2. Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)

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

  3. Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Amazon S3 PHP Class Documentation

    API : http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation Example: http://www.phpb ...

  2. FineUI 框架,RIA 富客户端应用的选择

    FineUI 框架演示地址:http://www.fineui.com/demo/ 是asp.net 和extjs 结合的框架,可以快速创建企业应用程序的界面,节省开发时间,具体使用详见fineUI ...

  3. 将开始我的WebForm控件开发之旅

    时间总是过得很快,一转眼三个月就过去了,三个月内发生了很多的事.因为学校的学习,离开了我入门WPF的公司:开发了第一个外包项目,做的是WebForm的:而且了马上要毕业了,毕业后的公司应该是专门用We ...

  4. 【转载】MySQL 5.6主从Slave_IO_Running:Connecting/error connecting to master *- retry

    原文地址:MySQL 5.6主从Slave_IO_Running:Connecting/error connecting to master *- retry 作者:忆雨林枫 刚配置的MySQL主从, ...

  5. php正则表达式判断是否为ip格式

    <?php $a = '127.0.0.111'; $b = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/",$a); ...

  6. c#之委托总结

    1.委托的声明和编译原理 声明委托: delegate void Translate(string str); 通过反编译之后可得下面代码 private sealed class Translate ...

  7. 编译时和运行时、OC中对象的动态编译机制

    编译时 编译时顾名思义就是正在编译的时候.那啥叫编译呢?就是编译器帮你把源代码翻译成机器能识别的代码.(当然只是一般意义上这么说,实际上可能只是翻译成某个中间状态的语言.比如Java只有JVM识别的字 ...

  8. 保持程序在后台长时间运行-b

    iOS为了让设备尽量省电,减少不必要的开销,保持系统流畅,因而对后台机制采用墓碑式的“假后台”.除了系统官方极少数程序可以真后台,一般开发者开发出来的应用程序后台受到以下限制:1.用户按Home之后, ...

  9. asp.net 中插入数据到access

    这样报语法错误: insert into Menu_wx(userid,menutype,MenuID,menuname,MenuTitle,Url,Key,OrderValue,State) val ...

  10. int a[5]={1,2,3,4,5}; int *p=(int*)(&a+1); printf("%d",*(p-1)); 答案为什么是5?

    这个问题的关键是理解 &a a是一个数组名,也就是数组的首地址.对a进行取地址运算符,得到的是一个指向数组的指针!!!!这句话尤为重要!也就相当于int (*p) [5] = &a;p ...