一、

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. php intval()函数

    格式:int intval(mixed $var [, int $base]); 1.intval()的返回值是整型,1或者0.可作用于数组或者对象(对象报错信息:Notice: Object of ...

  2. 每日一“酷”之pprint

    pprint – 美观打印 作用:美观打印数据结构 pprint 包含一个“美观打印机”,用于生成数据结构的一个美观视图.格式化工具会生成数据结构的一些表示,不仅可以由解释器正确地解析,而且便于人类阅 ...

  3. c#中的结构与枚举

    结构 与c++不同的是,结构应该定义在命名空间或者类里面,成员变量叫字段,字段并且有访问控制符,每个字段前要加一个下划线 例子 using System; using System.Collectio ...

  4. 谈网页游戏外挂之用python模拟游戏(热血三国2)登陆

    看web看多了,想写写页游的外挂,其实原理是一样的,就是端口不一样协议字段你不知道,而这也提高了点技术门槛,看我们来一点一点突破这些门槛,这次我们来用python发包模拟flash的客户端登陆. 以热 ...

  5. 【转】Eazfuscator.NET 3.3中混淆化需要注意的一些问题

    对于DLL,Eazfuscator.NET默认不会混淆化任何公共成员,因为类库的公共成员很有可能被外界调用,而对于EXE的程序集,所有类型都可能被混淆化.注意上面这句话有一个“可能”,因为Eazfus ...

  6. 世界级Oracle专家Jonathan Lewis:我很为DBA们的未来担心(图灵访谈)

    部分节选 图灵社区:如果您的子女对计算机科学感兴趣,你会让他们选什么具体的方向呢? JL:我的孩子对计算机科学一点都不感兴趣,甚至对科学都没什么兴趣.他们主修的都是艺术.如果要我给其他的人建议的话,我 ...

  7. WPF中的Style

    一.Style基础知识 构成Style最重要的两种元素是Setter和Trigger Setter类帮助我们设置控件的静态外观风格 Trigger类帮助我们设置控件的行为风格 Setter类的Prop ...

  8. DC-EPC小结

    今晚上修完了最后2个学时的EPC(课程主页),这意味着本学期的DC和共20个学时的EPC到此结束,这有可能是我人生中最后一次上英语课. Tom是我DC课的老师,EPC起于Tom和Micheal的Deb ...

  9. Unable to create the store directory. (Exception from HRESULT: 0x80131468)

    一个ASP.NET的程序,使用了MS ReportViewer报告控件,在用该控件导出生成Excel文件时,先是提示行不能超过65535. 这个是由于Excel2003的行限制的原因.由于修改成用Ex ...

  10. uImage、zImage、bzImage、vlinzx区别

    在网络中,不少服务器采用的是Linux系统.为了进一步提高服务器的性能,可能需要根 据特定的硬件及需求重新编译Linux内核.编译Linux 内核,需要根据规定的步骤进行,编译内核过程中涉及到几个重要 ...