映射实体关联关系
一 映射 一对一
组件类是没有OID没有对应的表 生命周期也依赖于其他的对象
两种方法
1 按照外键映射
2 按照主键映射
1 按照外键映射
定义一个或多个外键 参照 其他表的主键
例子
Customer和Address
Customer.hbm.xml
<?
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Customer"
table
="CUSTOMERS"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="name"
column
="NAME"
type
="string"
/>
<
many-to-one
name
="homeAddress"
column
="HOME_ADDRESS_ID"
class
="ergal.Address"
casecade
="all"
unique
="true"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
many-to-one
name
="comAddress"
column
="COM_ADDRESS_ID"
class
="ergal.Address"
casecade
="all"
unique
="true"
/>
</
class
>
</
hibernate-mapping
>
其中unique="true" 表明每个 Customer只有唯一的Address
Address.hbm.xml
<?
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Address"
table
="ADDRESSES"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="native"
/>
</
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
="customer"
class
="ergal.Address"
property-ref
="homeAddress"
/>
</
class
>
</
hibernate-mapping
>
其中property-ref 表明建立了从homeAddress和Customer的关联
注意:
这里one-to-one只能用一次 所以不能同时让homeAddress和comAddress都和Customer双向关联
可以用继承的方式来解决这种映射的不足
2 按照主键映射
一方的ID字段几既是主键 也作为外键参照是另一方的主键
适用范围这种方式是只是纯粹的一对一 只存在一个表的一个字段对应另一个表
例子
Customer和Address
Customer.hbm.xml
Customer.hbm.xml
<?
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Customer"
table
="CUSTOMERS"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="name"
column
="NAME"
type
="string"
/>
<
one-to-one
name
="address"
class
="ergal.Address"
casecade
="all"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
</
class
>
</
hibernate-mapping
>
Address.hbm.xml
<?
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Address"
table
="ADDRESSES"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="foreign"
>
<
param
name
="property"
>
customer
</
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
="customer"
class
="ergal.Customer"
constrained
="true"
/>
</
class
>
</
hibernate-mapping
>
这里的id有点不一样 和Customer共享 所以 要用foreign
而且要添加<param name="property">customer</param>元素
在one-to-one中 有constrained属性表明主键同时作为外键参照 Customer表
二 映射 单向多对多
例子
Category和Item多对多
现在只允许Category导航到Item
Category.hbm.xml
<?
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Category"
table
="CATEGORIES"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="name"
column
="NAME"
type
="string"
/>
<
set
name
="items"
table
="CATEGORY_ITEM"
lazy
="true"
cascade
="save-update"
>
<
key
column
="CATEGORY_ID"
/>
<
many-to-many
class
="ergal.Item"
column
="ITEM_ID"
/>
</
set
>
</
class
>
</
hibernate-mapping
>
这里会多一个表用来做另外两个表的参照
用联合主键 两个字段分别作为外键参照两个表的主键
Item.hbm.xml
<?
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Item"
table
="ITEMS"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="name"
column
="NAME"
type
="string"
/>
<
property
name
="basePrice"
column
="BASE_PRICE"
type
="double"
/>
</
class
>
</
hibernate-mapping
>
hbm2ddl
会有3个表产生
CATEGORIES
1 - ID
2 - NAME
CATEGORY_ITEM
1 - CATEGORY_ID
2 - ITEM_ID
ITEM
1 - ID
2 - NAME
3 - BASE_PRICE
测试代码
BusinessService.java
package
ergal;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.
*
;
import
org.hibernate.
*
;
import
org.hibernate.cfg.
*
;
import
java.sql.
*
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
class
BusinessService
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public static SessionFactory sessionFactory;
static
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Configuration config=new Configuration();
sessionFactory=config.configure().buildSessionFactory();
}
catch(Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
e.printStackTrace();
}
}
public void saveCategory(Category category)throws Exception
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Session session=sessionFactory.openSession();
Transaction tx=null;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tx=session.beginTransaction();
session.save(category);
tx.commit();
}
catch(Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(tx!=null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tx.rollback();
}
throw e;
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
session.close();
}
}
public Category loadCategory(Long id)throws Exception
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Session session=sessionFactory.openSession();
Transaction tx=null;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tx=session.beginTransaction();
Category category=(Category)session.load(Category.class, id);
Hibernate.initialize(category.getItems());
tx.commit();
return category;
}
catch(Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(tx!=null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
tx.rollback();
}
throw e;
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
session.close();
}
}
public void printCategory(Category category)throws Exception
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Set items=category.getItems();
Iterator it=items.iterator();
while(it.hasNext())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Item item=(Item)it.next();
System.out.println(category.getName()+" " +item.getName()+ " " +item.getBasePrice());
}
}
public void test()throws Exception
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Item item1=new Item("Nokia6610", 3200);
Item item2=new Item("Nokia3230", 2400);
Item item3=new Item("ATI X1600", 1500);
Item item4=new Item("Novida 6800GT", 1600);
Category category1=new Category();
category1.setName("Moblie");
category1.getItems().add(item1);
category1.getItems().add(item2);
Category category2=new Category();
category2.setName("Displaycard");
category2.getItems().add(item3);
category2.getItems().add(item4);
Category category3=new Category();
category3.setName("Nokia series");
category3.getItems().add(item1);
category3.getItems().add(item2);
saveCategory(category1);
saveCategory(category2);
saveCategory(category3);
Category c1=loadCategory(new Long(1));
printCategory(c1);
Category c2=loadCategory(new Long(2));
printCategory(c2);
Category c3=loadCategory(new Long(3));
printCategory(c3);
}
public static void main(String[] args)throws Exception
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.out.println("----------------test start---------------");
new BusinessService().test();
sessionFactory.close();
System.out.println("----------------test end---------------");
}
}
结果
[java] ----------------test start---------------
[java] Hibernate: insert into CATEGORIES (NAME) values (?)
[java] Hibernate: insert into ITEMS (NAME, BASE_PRICE) values (?, ?)
[java] Hibernate: insert into ITEMS (NAME, BASE_PRICE) values (?, ?)
[java] Hibernate: insert into CATEGORY_ITEM (CATEGORY_ID, ITEM_ID) values (
?, ?)
[java] Hibernate: insert into CATEGORY_ITEM (CATEGORY_ID, ITEM_ID) values (
?, ?)
[java] Hibernate: insert into CATEGORIES (NAME) values (?)
[java] Hibernate: insert into ITEMS (NAME, BASE_PRICE) values (?, ?)
[java] Hibernate: insert into ITEMS (NAME, BASE_PRICE) values (?, ?)
[java] Hibernate: insert into CATEGORY_ITEM (CATEGORY_ID, ITEM_ID) values (
?, ?)
[java] Hibernate: insert into CATEGORY_ITEM (CATEGORY_ID, ITEM_ID) values (
?, ?)
[java] Hibernate: insert into CATEGORIES (NAME) values (?)
[java] Hibernate: update ITEMS set NAME=?, BASE_PRICE=? where ID=?
[java] Hibernate: update ITEMS set NAME=?, BASE_PRICE=? where ID=?
[java] Hibernate: insert into CATEGORY_ITEM (CATEGORY_ID, ITEM_ID) values (
?, ?)
[java] Hibernate: insert into CATEGORY_ITEM (CATEGORY_ID, ITEM_ID) values (
?, ?)
[java] Hibernate: select category0_.ID as ID0_0_, category0_.NAME as NAME0_
0_ from CATEGORIES category0_ where category0_.ID=?
[java] Hibernate: select items0_.CATEGORY_ID as CATEGORY1_1_, items0_.ITEM_
ID as ITEM2_1_, item1_.ID as ID2_0_, item1_.NAME as NAME2_0_, item1_.BASE_PRICE
as BASE3_2_0_ from CATEGORY_ITEM items0_ left outer join ITEMS item1_ on items0_
.ITEM_ID=item1_.ID where items0_.CATEGORY_ID=?
[java] Moblie Nokia6610 3200.0
[java] Moblie Nokia3230 2400.0
[java] Hibernate: select category0_.ID as ID0_0_, category0_.NAME as NAME0_
0_ from CATEGORIES category0_ where category0_.ID=?
[java] Hibernate: select items0_.CATEGORY_ID as CATEGORY1_1_, items0_.ITEM_
ID as ITEM2_1_, item1_.ID as ID2_0_, item1_.NAME as NAME2_0_, item1_.BASE_PRICE
as BASE3_2_0_ from CATEGORY_ITEM items0_ left outer join ITEMS item1_ on items0_
.ITEM_ID=item1_.ID where items0_.CATEGORY_ID=?
[java] Displaycard Novida 6800GT 1600.0
[java] Displaycard ATI X1600 1500.0
[java] Hibernate: select category0_.ID as ID0_0_, category0_.NAME as NAME0_
0_ from CATEGORIES category0_ where category0_.ID=?
[java] Hibernate: select items0_.CATEGORY_ID as CATEGORY1_1_, items0_.ITEM_
ID as ITEM2_1_, item1_.ID as ID2_0_, item1_.NAME as NAME2_0_, item1_.BASE_PRICE
as BASE3_2_0_ from CATEGORY_ITEM items0_ left outer join ITEMS item1_ on items0_
.ITEM_ID=item1_.ID where items0_.CATEGORY_ID=?
[java] Nokia series Nokia6610 3200.0
[java] Nokia series Nokia3230 2400.0
[java] 02:09:05,875 INFO SessionFactoryImpl:767 - closing
[java] ----------------test end---------------
这里也可以根据储存的元素是否有重复 排序 检索的策略等等选择其他的集合类型 如:<idbag> <list> <map>
具体参照集合映射
三 映射 双向多对多
必须把一方的inverse设置为true 两端都可以使用<set>元素
inverse属性为false的一方可以用<idbag> <list> <map>
inverse属性为true的一方可以用<idbag>
1 两端使用<set>元素
例子同上
Category和Item多对多
现在只允许Category导航到Item
Category.hbm.xml
不变
<?
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Category"
table
="CATEGORIES"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="name"
column
="NAME"
type
="string"
/>
<
set
name
="items"
table
="CATEGORY_ITEM"
lazy
="true"
cascade
="save-update"
>
<
key
column
="CATEGORY_ID"
/>
<
many-to-many
class
="ergal.Item"
column
="ITEM_ID"
/>
</
set
>
</
class
>
</
hibernate-mapping
>
Item.hbm.xml
<?
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Item"
table
="ITEMS"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="name"
column
="NAME"
type
="string"
/>
<
property
name
="basePrice"
column
="BASE_PRICE"
type
="double"
/>
<
set
name
="categories"
table
="CATEGORY_ITEM"
lazy
="true"
inverse
="true"
cascade
="save-update"
>
<
key
column
="ITEM_ID"
/>
<
many-to-many
class
="ergal.Category"
column
="CATEGORY_ID"
/>
</
set
>
</
class
>
</
hibernate-mapping
>
添加了set元素
hbm2ddl产生的表和单向时时一样的
可以根据需要修改Item的构造函数
测试为了演示 这种双向关联的 两种导航都时可以的
测试代码 可以这么写
Item item1
=
new
Item(
"
Nokia6610
"
,
3200
);
Item item2
=
new
Item(
"
Nokia3230
"
,
2400
);
Item item3
=
new
Item(
"
ATI X1600
"
,
1500
);
Item item4
=
new
Item(
"
Novida 6800GT
"
,
1600
);
Category category1
=
new
Category();
category1.setName(
"
Moblie
"
);
category1.getItems().add(item1);
category1.getItems().add(item2);
item1.getCategories().add(category1);
item2.getCategories().add(category1);
Category category2
=
new
Category();
category2.setName(
"
Displaycard
"
);
category2.getItems().add(item3);
category2.getItems().add(item4);
item3.getCategories().add(category2);
item4.getCategories().add(category2);
Category category3
=
new
Category();
category3.setName(
"
Nokia series
"
);
category3.getItems().add(item1);
category3.getItems().add(item2);
item1.getCategories().add(category3);
item2.getCategories().add(category3);
saveCategory(category1);
saveCategory(category2);
saveCategory(category3);
Category c1
=
loadCategory(
new
Long(
1
));
printCategory(c1);
Category c2
=
loadCategory(
new
Long(
3
));
printCategory(c2);
Category c3
=
loadCategory(
new
Long(
4
));
printCategory(c3);
注意
Category的Id在 执行第三个操作的时候被更新了
就是在把item1和item2导航到第三个Category时 他的ID变了
原来的ID不能导航到他的 items了
所以修改了ID的 数字才能正确查找到Nokia Series
虽然在数据库中的ID 2 仍然为Nokia Series 但是已经无法导航到正确的Items了
结果为
[java] ----------------test start---------------
[java] Hibernate: select category0_.ID as ID0_0_, category0_.NAME as NAME0_
0_ from CATEGORIES category0_ where category0_.ID=?
[java] Hibernate: select items0_.CATEGORY_ID as CATEGORY1_1_, items0_.ITEM_
ID as ITEM2_1_, item1_.ID as ID2_0_, item1_.NAME as NAME2_0_, item1_.BASE_PRICE
as BASE3_2_0_ from CATEGORY_ITEM items0_ left outer join ITEMS item1_ on items0_
.ITEM_ID=item1_.ID where items0_.CATEGORY_ID=?
[java] Moblie Nokia3230 2400.0
[java] Moblie Nokia6610 3200.0
[java] Hibernate: select category0_.ID as ID0_0_, category0_.NAME as NAME0_
0_ from CATEGORIES category0_ where category0_.ID=?
[java] Hibernate: select items0_.CATEGORY_ID as CATEGORY1_1_, items0_.ITEM_
ID as ITEM2_1_, item1_.ID as ID2_0_, item1_.NAME as NAME2_0_, item1_.BASE_PRICE
as BASE3_2_0_ from CATEGORY_ITEM items0_ left outer join ITEMS item1_ on items0_
.ITEM_ID=item1_.ID where items0_.CATEGORY_ID=?
[java] Displaycard ATI X1600 1500.0
[java] Displaycard Novida 6800GT 1600.0
[java] Hibernate: select category0_.ID as ID0_0_, category0_.NAME as NAME0_
0_ from CATEGORIES category0_ where category0_.ID=?
[java] Hibernate: select items0_.CATEGORY_ID as CATEGORY1_1_, items0_.ITEM_
ID as ITEM2_1_, item1_.ID as ID2_0_, item1_.NAME as NAME2_0_, item1_.BASE_PRICE
as BASE3_2_0_ from CATEGORY_ITEM items0_ left outer join ITEMS item1_ on items0_
.ITEM_ID=item1_.ID where items0_.CATEGORY_ID=?
[java] Nokia series Nokia3230 2400.0
[java] Nokia series Nokia6610 3200.0
[java] 03:18:11,125 INFO SessionFactoryImpl:767 - closing
[java] ----------------test end---------------
inverse属性为false的一方可以用<idbag> <list> <map>
inverse属性为true的一方可以用<idbag>
不做详细的演示了
可能都会存在或多或少的这种意想不到的差异
映射一对多关联关系
在关系型数据库中 只存在外键参照关系 总是由many方参照one方 所以关系型数据库只支持多对一或者一对一的单向关联
<many-to-one>建立一对多单向关联
内嵌的以下属性
' name 设定持久化类的属性名
' column 设定和持久化类对应的表的外键
' class 设定持久化类的属性
' not-null 不允许为空
' cascade
当取值为 save-update时 表示级联保存更新与他相关联的对象
当取值为none时 Hibernate删除一个对象的时候不会级联删除与他相关联的对象
当取值为delete的时候才会级联删除
当取值为all-delete-orphan时会
' 当保存或更新对象时级联保存或更新相关联的的对象相当于save-update
' 当删除对象时会级联删除相关联的对象相当于delete
' 删除不再和one方相关联的所有many方对象
<set>建立一对多双向关联
属性
' name 映射持久化类的属性名
' cascade 当取值为 save-update时 表示级联保存更新与他相关联的对象
当取值为 delete时 表示级联删除更新与他相关联的对象
' inverse 属性 如果为true
表明在双向关联中 one的一方只是many一方的关联的镜像 当Hibernate检测到两方的状态均发生变化的时候 就会仅仅按照many方来同步更新数据库
这样做可以提高应用的性能
注意两点:
one方的属性inverse 因该设置为true
最好同时修改两方的属性
like this:
order.setCustomer(customer);
customer.getOrders().add(order);
当接触双方关系的时候也要一起删除
like this
order.setCustomer(null);
customer.getOrders().remove(order);
<key column=> 通过的表的外键参照
<one-to-many class=> 表明里面放的是一组class对象
Hibernate会自动清理缓存中的所有持久化对象,按照持久化对象状态的改变来改变同步更新数据库
Hibernate 的一对多自身关联
在同一个类也可以自身一对多关联
如下
Category.hbm.xml
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
hibernate-mapping
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Category"
table
="CATEGORIES"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="increment"
/>
</
id
>
<
property
name
="name"
type
="string"
>
<
column
name
="NAME"
length
="15"
/>
</
property
>
<
set
name
="childCategories"
cascade
="save-update"
inverse
="true"
>
<
key
column
="CATEGORY_ID"
/>
<
one-to-many
class
="ergal.Category"
/>
</
set
>
<
many-to-one
name
="parentCategory"
column
="CATEGORY_ID"
class
="ergal.Category"
/>
</
class
>
</
hibernate-mapping
>
但是在在数据表类的CATEGORY_ID只是表现parentCategory的
换句话说就是还是和以前一样many方的<many-to-one>在表中有实体
另外在论坛中有人告诉我说
1. the "find X as x" syntax is deprecated. Use "find X x" instead.
2. Don't build query strings like that. Use positional or named parameters. This is better:
List results
=
session.createQuery(
"
from Category where name=:name
"
).setString(
"
name
"
, name).list();
所以查询语句这么写更好
public
Category findCategoryByName(Session session, String name)
throws
Exception
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
Category c=new Category();
List results = session.createQuery("from Category where name=:name").setString("name", name).list();
Iterator it = results.iterator();
while(it.hasNext())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
c = (Category)it.next();
}
return c;
}
映射组成关系
通过域模型(由程序代码组成)细化持久化类 提高代码的可重用性
<component>组件
属性:
name 持久化类的属性名
class 类名
元素:
<parent> 指定所属的整体类
<property> 内部的属性
值类型 和 实体类型
值类型没有OID 不能被单独持久化 实体类可以单独持久化
' 当程序持久化组件的所属类的时候 组件类会自动持久化 Hibernate是不允许单独持久化组件类的
' 组件可以嵌套 可以关联其他持久化类 其他持久化类不能关联它
例子
Computer.hbm.xml
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
hibernate-mapping
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Computer"
table
="COMPUTERS"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="increment"
/>
</
id
>
<
property
name
="name"
type
="string"
>
<
column
name
="NAME"
length
="15"
/>
</
property
>
<
component
name
="cpuBox"
class
="ergal.CpuBox"
>
<
parent
name
="computer"
/>
<
property
name
="type"
type
="string"
>
<
column
name
="CPUBOX_TYPE"
length
="15"
/>
</
property
>
<
component
name
="graphicsCard"
class
="ergal.GraphicsCard"
>
<
parent
name
="cpuBox"
/>
<
property
name
="type"
type
="string"
>
<
column
name
="GRAPHICSCARD_TYPE"
length
="15"
/>
</
property
>
</
component
>
<
many-to-one
name
="vendor"
column
="CPUBOX_VENDOR_ID"
class
="ergal.Vendor"
not-null
="true"
/>
</
component
>
</
class
>
</
hibernate-mapping
>
Vendor.hbm.xml
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
hibernate-mapping
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Vendor"
table
="VENDORS"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="increment"
/>
</
id
>
<
property
name
="name"
type
="string"
>
<
column
name
="NAME"
length
="15"
/>
</
property
>
</
class
>
</
hibernate-mapping
>
Customer.hbm.xml
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
hibernate-mapping
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
class
name
="ergal.Customer"
table
="CUSTOMERS"
lazy
="true"
>
<
id
name
="id"
type
="long"
column
="ID"
>
<
generator
class
="increment"
/>
</
id
>
<
property
name
="name"
type
="string"
>
<
column
name
="NAME"
length
="15"
/>
</
property
>
<
component
name
="homeAddress"
class
="ergal.Address"
>
<
parent
name
="customer"
/>
<
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
="ergal.Address"
>
<
parent
name
="customer"
/>
<
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
>
运行hbm2java
工具会产生六个类
Address.java
package
ergal;
//
Generated 2006-8-18 17:26:10 by Hibernate Tools 3.2.0.beta6a
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* Address generated by hbm2java
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Address
implements
java.io.Serializable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Fields
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private String province;
private String city;
private String street;
private String zipcode;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Constructors
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** default constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Address() ...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** full constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Address(String province, String city, String street, String zipcode) ...{
this.province = province;
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
// Property accessors
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getProvince() ...{
return this.province;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setProvince(String province) ...{
this.province = province;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getCity() ...{
return this.city;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setCity(String city) ...{
this.city = city;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getStreet() ...{
return this.street;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setStreet(String street) ...{
this.street = street;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getZipcode() ...{
return this.zipcode;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setZipcode(String zipcode) ...{
this.zipcode = zipcode;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
Computer.java
package
ergal;
//
Generated 2006-8-18 17:26:10 by Hibernate Tools 3.2.0.beta6a
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* Computer generated by hbm2java
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Computer
implements
java.io.Serializable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Fields
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private long id;
private String name;
private CpuBox cpuBox;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Constructors
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** default constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Computer() ...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** minimal constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Computer(CpuBox cpuBox) ...{
this.cpuBox = cpuBox;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** full constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Computer(String name, CpuBox cpuBox) ...{
this.name = name;
this.cpuBox = cpuBox;
}
// Property accessors
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public long getId() ...{
return this.id;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setId(long id) ...{
this.id = id;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getName() ...{
return this.name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setName(String name) ...{
this.name = name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public CpuBox getCpuBox() ...{
return this.cpuBox;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setCpuBox(CpuBox cpuBox) ...{
this.cpuBox = cpuBox;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
CpuBox.java
package
ergal;
//
Generated 2006-8-18 17:26:10 by Hibernate Tools 3.2.0.beta6a
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* CpuBox generated by hbm2java
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
CpuBox
implements
java.io.Serializable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Fields
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private String type;
private GraphicsCard graphicsCard;
private Vendor vendor;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Constructors
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** default constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public CpuBox() ...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** minimal constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public CpuBox(Vendor vendor) ...{
this.vendor = vendor;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** full constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public CpuBox(String type, GraphicsCard graphicsCard, Vendor vendor) ...{
this.type = type;
this.graphicsCard = graphicsCard;
this.vendor = vendor;
}
// Property accessors
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getType() ...{
return this.type;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setType(String type) ...{
this.type = type;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public GraphicsCard getGraphicsCard() ...{
return this.graphicsCard;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setGraphicsCard(GraphicsCard graphicsCard) ...{
this.graphicsCard = graphicsCard;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Vendor getVendor() ...{
return this.vendor;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setVendor(Vendor vendor) ...{
this.vendor = vendor;
}
}
Customer.java
package
ergal;
//
Generated 2006-8-18 17:26:10 by Hibernate Tools 3.2.0.beta6a
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* Customer generated by hbm2java
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Customer
implements
java.io.Serializable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Fields
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private long id;
private String name;
private Address homeAddress;
private Address comAddress;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Constructors
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** default constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Customer() ...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** full constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Customer(String name, Address homeAddress, Address comAddress) ...{
this.name = name;
this.homeAddress = homeAddress;
this.comAddress = comAddress;
}
// Property accessors
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public long getId() ...{
return this.id;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setId(long id) ...{
this.id = id;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getName() ...{
return this.name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setName(String name) ...{
this.name = name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Address getHomeAddress() ...{
return this.homeAddress;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setHomeAddress(Address homeAddress) ...{
this.homeAddress = homeAddress;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Address getComAddress() ...{
return this.comAddress;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setComAddress(Address comAddress) ...{
this.comAddress = comAddress;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
GraphicsCard.java
package
ergal;
//
Generated 2006-8-18 17:26:10 by Hibernate Tools 3.2.0.beta6a
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* GraphicsCard generated by hbm2java
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
GraphicsCard
implements
java.io.Serializable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Fields
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private String type;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Constructors
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** default constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public GraphicsCard() ...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** full constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public GraphicsCard(String type) ...{
this.type = type;
}
// Property accessors
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getType() ...{
return this.type;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setType(String type) ...{
this.type = type;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
Vendor.java
package
ergal;
//
Generated 2006-8-18 17:26:10 by Hibernate Tools 3.2.0.beta6a
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* Vendor generated by hbm2java
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Vendor
implements
java.io.Serializable
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Fields
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private long id;
private String name;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Constructors
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** default constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Vendor() ...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//** full constructor */
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Vendor(String name) ...{
this.name = name;
}
// Property accessors
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public long getId() ...{
return this.id;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setId(long id) ...{
this.id = id;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String getName() ...{
return this.name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setName(String name) ...{
this.name = name;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
运行hbm2ddl
生成以下ddl
create table COMPUTERS (
ID bigint not null,
NAME varchar(15),
CPUBOX_TYPE varchar(15),
GRAPHICSCARD_TYPE varchar(15),
CPUBOX_VENDOR_ID bigint not null,
primary key (ID));
create table CUSTOMERS (
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 VENDORS (
ID bigint not null,
NAME varchar(15),
primary key (ID));
alter table COMPUTERS
add index FK52FE749856F75DC7 (CPUBOX_VENDOR_ID),
add constraint FK52FE749856F75DC7 foreign key (CPUBOX_VENDOR_ID) references VENDORS (ID);