Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)
一、
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>)的更多相关文章
- Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- 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逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
随机推荐
- ASP.NET MVC 2 验证
来源:http://www.cnblogs.com/jhxk/articles/2612885.html 只为把自己觉的好的存起来 对用户输入的验证以及强制业务规则/逻辑是大多数web应用的核心需求 ...
- JavaScript创建对象的写法
JavaScript 有Date.Array.String等这样的内置对象,功能强大使用简单,人见人爱,但在处理一些复杂的逻辑的时候,内置对象就很无力了,往往需要开发者自定义对象. 对象是什么 从 ...
- Spark菜鸟学习营Day6 分布式代码运行调试
Spark菜鸟学习营Day6 分布式代码运行调试 作为代码调试,一般会分成两个部分 语法调试,也就是确定能够运行 结果调试,也就是确定程序逻辑的正确 其实这个都离不开运行,所以我们说一下如何让开发的S ...
- python操作mysql之pymysql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11.mys ...
- Windows 8.1 (64bit) 下搭建 Scrapy 0.22 环境
我的Windows 8.1 环境 1.下载安装Python 2.7.6 在Python官方网站中下载Python2.7.6的Windows安装包,根据默认配置安装到C:\Python27目录. 安装完 ...
- 深入剖析——float之个人见解
浮动的原本作用仅仅是为了实现文字的环绕效果. 以下分别是html与css代码,显示效果如下图.因为两个div使用了float浮动属性,所以脱离了标准文档流.让父元素撑开高度,我们需要清除浮动. < ...
- Eclipse中查看JDK类库的源代码
在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...
- 在IOS中使用json
1.从https://github.com/stig/json-framework/中下载json框架:json-framework 2.解压下载的包,将class文件夹下的所有文件导入到当前工程下. ...
- 2016 系统设计第一期 (档案一)MVC bootstrap model弹出子页面
通过bootstrap 弹出modal-dialog 子页面 ,例如我要弹出子页面:areaitem_sub_One.html. 具体步骤如下: 第一步:新建 areaitem_sub_One.ht ...
- hadoop 数据采样
http://www.cnblogs.com/xuxm2007/archive/2012/03/04/2379143.html 原文地址如上: 关于Hadoop中的采样器 .为什么要使用采样器 在这个 ...