[转]hibernate 集合类(Collections)映射(四)- map
四、Map集合映射
?
Map集合属性不仅需要映射属性value,还需要映射属性key。这里假设Employee的name属性是唯一的,如下修改Employee.hbm.xml配置文件中的name属性,设置unique='true':
?
Java代码
<property name="name"?
<property name="name"?
Java代码
public?
?????????//setter和getter方法??
?
}?
?
public?
?????????//setter和getter方法
?
}?
Xml代码
<?xml version="1.0"?>?
<!DOCTYPE hibernate-mapping PUBLIC???
??????<hibernate-mapping?????????????????????????????????????????????????????????????????????????????????????</hibernate-mapping>?
?
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
??????<hibernate-mapping?????????????????????????????????????????????????????????????????????</hibernate-mapping>?
Java代码
//List<Employee> list= new ArrayList<Employee>();??
//?????????//??????????????????????????????????????????????????????
//List<Employee> list= new ArrayList<Employee>();
//????????//?????????????????????????????????????????????????
emps:{employee1 name1?
数据库表中记录如下所示(未发生变化):
?
mysql> select * from department;
+----+-----------------+
| id | name???????????+----+-----------------+
|?+----+-----------------+
1 row in set (0.00 sec)
?
mysql> select * from employee;
+----+-----------------+-----------+
| id | name???????????+----+-----------------+-----------+
|?|?+----+-----------------+-----------+
2 rows in set (0.00 sec)