四、Map集合映射
Map集合属性不仅需要映射属性value,还需要映射属性key。这里假设Employee的name属性是唯一的,如下修改Employee.hbm.xml配置文件中的name属性,设置unique='true':
Java代码
<property name="name" unique="true"/>
<property name="name" unique="true"/> 实体类Department如下:
Java代码
public class Department {
private int id;
private String name;
private Map<String, Employee> emps;
//setter和getter方法
}
public class Department {
private int id;
private String name;
private Map<String, Employee> emps;
//setter和getter方法
} 修改Department.hbm.xml配置文件如下:
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 package="com.reiyen.hibernate.domain">
<class name="Department">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<map name="emps">
<key column="depart_id" />
<map-key type="string" column="name" />
<one-to-many class="Employee" />
</map>
</class>
</hibernate-mapping>
<?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 package="com.reiyen.hibernate.domain">
<class name="Department">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<map name="emps">
<key column="depart_id" />
<map-key type="string" column="name" />
<one-to-many class="Employee" />
</map>
</class>
</hibernate-mapping> 将测试类下如下注释部分(即List部分)替换,改成Map重新进行测试:
Java代码
//List<Employee> list= new ArrayList<Employee>();
// list.add(employee2);
// list.add(employee1);
Map<String,Employee> emps = new HashMap<String,Employee>();
emps.put(employee1.getName(), employee1);
emps.put(employee2.getName(), employee2);
depart.setEmps(emps);
//List<Employee> list= new ArrayList<Employee>();
// list.add(employee2);
// list.add(employee1);
Map<String,Employee> emps = new HashMap<String,Employee>();
emps.put(employee1.getName(), employee1);
emps.put(employee2.getName(), employee2);
depart.setEmps(emps); 测试结果如下:
emps:{employee1 name1 =id=1 name=employee1 name1, employee2 name2 =id=2 name=employee2 name2}(红色标记部分为key部分)
数据库表中记录如下所示(未发生变化):
mysql> select * from department;
+----+-----------------+
| id | name |
+----+-----------------+
| 1 | department name |
+----+-----------------+
1 row in set (0.00 sec)
mysql> select * from employee;
+----+-----------------+-----------+
| id | name | depart_id |
+----+-----------------+-----------+
| 1 | employee1 name1 | 1 |
| 2 | employee2 name2 | 1 |
+----+-----------------+-----------+
2 rows in set (0.00 sec)
相关推荐
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引...
7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3. ...
集合类(Collections)映射 6.1. 持久化集合类(Persistent Collections) 6.2. 映射集合(Mapping a Collection) 6.3. 值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. ...
<composite-map-key>元素允许你映射一个组件类作为一个Map的key,前提是你必须正确的在这个类中重写了hashCode() 和 equals()方法。 8.4. 组件作为联合标识符(Components as composite identifiers) 你可以使用一...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引...
7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3. ...
7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3...
6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引...
7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3. ...
集合类(Collections)映射 6.1. 持久化集合类(Persistent Collections) 6.2. 映射集合(Mapping a Collection) 6.3. 值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. ...
集合类(Collections)映射 6.1. 持久化集合类 6.2. 集合外键(Collection foreign keys) 6.3. 值集合于多对多关联(Collections of values and many-to-many associations) 6.4. 一对多关联 6.5. 延迟...
6. 集合类(Collections)映射 6.1. 持久化集合类 6.2. 集合外键(Collection foreign keys) 6.3. 值集合于多对多关联(Collections of values and many-to-many associations) 6.4. 一对多关联 6.5. 延迟加载 6.6. 有序...
1.2.1. 映射 Person 类 ................................................ 13 1.2.2. 单向 Set-based 的关联 ......................................... 14 1.2.3. 使关联工作 .....................................