Hibernate 集合属性List 的使用----Hibernate 学习笔记总结(一)
这是一个持久化类Person
package com.alpha.hibernate;
import java.util.ArrayList;
import java.util.List;
/*List 是有序集合,因此持久化到数据库时也必须增加一列来表示集合元素的次序。
看下面的持久化类,该Person 类有个集合属性: schools,该属性对应多个学校。而集合
属性只能以接口声明,因此在下面的代码中,schools 的类型只能是List,不能是ArrayList ,
但该集合属性必须使用实现类完成初始化。*/
public class Person
{
private int id;
private int age;
private String name;
@SuppressWarnings("unchecked")
private List schools = new ArrayList();
public Person()
{
}
...... //此处省略了 getter 和setter
@SuppressWarnings("unchecked")
public List getSchools()
{
return schools;
}
@SuppressWarnings("unchecked")
public void setSchools(List schools)
{
this.schools = schools;
}
}
这个是person.hbm.xm
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0 EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.alpha.hibernate">
<class name="Person" table="person" >
<id name="id" column="person_id" >
<generator class="identity"></generator>
</id>
<property name="name"/>
<property name="age"/>
<!-- list列表schools 配置 -->
<list name="schools" table="school">
<key column="personId" not-null="true"/>
<list-index column="list_order"/>
<element type="string" column="school_name"/>
</list>
</class>
</hibernate-mapping>