Set类型的使用:
<hibernate-mapping package="cn.model">
<class name="Department" table="DEPARTMENT">
<id name="id" column="ID">
<generator class="native"></generator>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<set name="employees">
<key column="DEPT_ID" />
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
Set是最常见的在Hibernate中对集合进行映射的类型,其配置也很简单;
List类型的使用:
<hibernate-mapping package="cn.model">
<class name="Department" table="DEPARTMENT">
<id name="id" column="ID">
<generator class="native"></generator>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<list name="employees">
<key column="DEPT_ID" />
<list-index column="EMPLOYEE_INDEX" />
<one-to-many class="Employee"/>
</list>
</class>
</hibernate-mapping>
使用List进行集合映射有一点特殊,List是按顺序进行存入所以Hibernate还需要在表中新增一个排序列来存入数据存入的顺序:
Hibernate:
create table EMPLOYEE (
ID integer not null auto_increment,
NAME varchar(255),
DEPT_ID integer,
EMPLOYEE_INDEX integer,
primary key (ID)
)
Bag类型的使用:
如果想使用List,但又不想在表中产生排序列,那可以使用Bag ;在实体中还是使用List,但在Hbm.xml映射文件中将list改为bag即可:
public class Department { private Integer id;
private String name;
private List<Employee> employees; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
} }
<hibernate-mapping package="cn.model">
<class name="Department" table="DEPARTMENT">
<id name="id" column="ID">
<generator class="native"></generator>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<bag name="employees">
<key column="DEPT_ID" />
<one-to-many class="Employee"/>
</bag>
</class>
</hibernate-mapping>
Map类型的使用:
使用Map类型对集合进行映射,程序中用的不多,但是也有存在,配置相比Set和Bag较麻烦一点:
<hibernate-mapping package="cn.model">
<class name="Department" table="DEPARTMENT">
<id name="id" column="ID">
<generator class="native"></generator>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<map name="employees">
<key column="DEPT_ID" />
<map-key type="string" column="name" />
<one-to-many class="Employee"/>
</map>
</class>
</hibernate-mapping>
数组Object[]类型的使用:
数组类型映射和List有点类似,都是需要一个排序的列:
public class Department { private Integer id;
private String name;
private Employee[] employees; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Employee[] getEmployees() {
return employees;
}
public void setEmployees(Employee[] employees) {
this.employees = employees;
} }
<hibernate-mapping package="cn.model">
<class name="Department" table="DEPARTMENT">
<id name="id" column="ID">
<generator class="native"></generator>
</id>
<property name="name" column="NAME" type="java.lang.String" />
<array name="employees">
<key column="DEPT_ID" />
<list-index column="EMPLOYEE_INDEX" />
<one-to-many class="Employee"/>
</array>
</class>
</hibernate-mapping>
总结: