---
title: "hibernate入门（九）：组件关系映射"
author: "cfeers"
date: "2010-08-26T10:51:17Z"
tags: []
categories: ["企业架构"]
slug: "hibernate"
draft: false
lastmod: "2010-08-26T10:51:17Z"
id: 747546
url: "https://cfeers.iteye.com/blog/747546"
---

5．组件关系映射：

典型实例：每个人有不同的名字，或者多个人可以是同一个名字，对于这种现象可以是“一个一”或者“多对一”，这样会被设计成两张表来映射，而我们采用组件关系来映射就可以在一张表中映射实现。具体过程如下：

&gt;&gt;步骤一、编写实体文件Name.java 和People.java

Name.java内容如下: 省略getXXX和setXXX()

Java代码 [![复制代码](http://86asm.iteye.com/images/icon_copy.gif)](http://86asm.iteye.com/blog/482209 "复制代码")

1. **package** com.asm.hibernate.domain;
2. **public** **class** Name {
3. **private** String firstName;
4. **private** String secondName;
5. }

```
package com.asm.hibernate.domain;
public class Name {
	private String firstName;
	private String secondName;
}
```

 

People.java内容如下: 省略getXXX和setXXX()

Java代码 [![复制代码](http://86asm.iteye.com/images/icon_copy.gif)](http://86asm.iteye.com/blog/482209 "复制代码")

1. **package** com.asm.hibernate.domain;
2. **public** **class** People {
3. **private** **int** id;
4. **private** Name name;
5. }

```
package com.asm.hibernate.domain;
public class People {
	private int id;
	private Name name;	
}
```

 

&gt;&gt;步骤二、编写配置文件People.hbm.xml 内容如下：

Java代码 [![复制代码](http://86asm.iteye.com/images/icon_copy.gif)](http://86asm.iteye.com/blog/482209 "复制代码")

01. &lt;hibernate-mapping
02. **package**="com.asm.hibernate.domain"&gt;
03. &lt;**class** name="People"&gt;
04. &lt;id name="id"&gt;
05. &lt;generator **class**="native"/&gt;
06. &lt;/id&gt;
07. &lt;component name="name"&gt;
08. &lt;property name="firstName" column="first\_name"&gt;&lt;/property&gt;
09. &lt;property name="secondName" column="second\_name"&gt;&lt;/property&gt;
10. &lt;/component&gt;
11. &lt;/**class**&gt;
12. &lt;/hibernate-mapping&gt;

```
<hibernate-mapping 
	package="com.asm.hibernate.domain">

	<class name="People">
		<id name="id">
			<generator class="native"/>
		</id>
	<component name="name">
		<property name="firstName" column="first_name"></property>
		<property name="secondName" column="second_name"></property>
	</component>
	</class>
</hibernate-mapping>
```

 

说明：尽管在上一步，说Name为实体配置文件，但这里并不为为它写配置文件，它实质相当于一个更一般化的属性，且这个属性是在People的实体配置文件中用〈component〉元素来进行配置的，它的name属性是随意随的有意义的名字，而其子元素分别为Name类的属性值。

&gt;&gt;步骤三、在主配置文件中关联此实体配置文件：

&lt;mapping resource="com/asm/hibernate/domain/People.hbm.xml"/&gt;

&gt;&gt;步骤四，编写测试类，内容如下：省略导入的包

Java代码 [![复制代码](http://86asm.iteye.com/images/icon_copy.gif)](http://86asm.iteye.com/blog/482209 "复制代码")

01. **package** com.asm.hibernate.test;
02. **public** **class** ComponentTest {
03. **public** **static** **void** main(String\[] args) {
04. Name n = **new** Name();
05. n.setFirstName("zhang");
06. n.setSecondName("san");
07. People p = **new** People();
08. p.setName(n);
09. add(p);
10. }
11. **static** **void** add(People p) {
12. Session s = **null**;
13. Transaction ts = **null**;
14. **try** {
15. s = HibernateUtil.getSession();
16. ts = s.beginTransaction();
17. s.save(p);
18. ts.commit();
19. } **catch** (HibernateException e) {
20. **if** (ts != **null**)
21. ts.rollback();
22. **throw** e;
23. } **finally** {
24. **if** (s != **null**)
25. s.close();
26. }
27. }
28. }

```
package com.asm.hibernate.test;
public class ComponentTest {

	public static void main(String[] args) {
		Name n = new Name();
		n.setFirstName("zhang");
		n.setSecondName("san");
		People p = new People();
		p.setName(n);		
		add(p);
	}

	static void add(People p) {
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.save(p);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}
}
```

 

//说明：这种映射较简单，执行后可以查看下表结构。

6.总结关联关系：

在理解Hibernate如何处理这些对象之间的关联关系的底层细节时，考虑如何实现把这种关系保存到数据库，考虑如可检索出关联对象。