list和set的使用方式差不多,
set需要两个属性:
private Set<Employee> emps;
 
< set name ="emps" >
         < key column ="depart_id" />
         < one-to-many class ="Employee" />
</ set >
 
而list需要增加一个list_index的属性,标识其加入位置
private List<Employee> emps;
< list name ="emps" >
         < key column ="depart_id" />
<!-- 这一列是Hibernate标识员工加入的位置 -->
         < list-index column ="order_column" />
         < one-to-many class ="Employee" />
</ list >
 
此时,由于数据表中多增加了一列"order_column"来保存排列位置,可以使用bag进行优化.
<b ag name ="emps" >
         < key column ="depart_id" /> 
         < one-to-many class ="Employee" />
</ bag >
 
注意:bag只能和list类型对于
 
而对于Map类型,使用如下:
private Map < String, Employee > emps; // key=Employee.name

< map name ="emps" >
         < key column ="depart_id" />
         < map0key type ="string" column ="name" />
         < one-to-many class ="Employee" />
</ map >
 
对于Map,需要指定名称map的key.
 
使用Hibernate较多的时候,推荐使用Set类型.
如果使用List,当不需要排序时,则使用bag进行映射.
 
另外,入库保存时定义的类型,与Hibernate查询结果的类型不能完全一致.
如Set<Employee> emps = new HashSet<Employee>();
如果查询后,进行转型;HashSet hs = (HashSet) depart.getEmployee();
会抛出类型转换的异常,Hibernate返回的类型是:org.hibernate.collection.PersistentSet
 
即,对于Hibernate,定义属性是,其类型必须是接口.
如:private Set<Employee> emps; 而不能定义成:private HashSet<Employee> emps;