Hibernate要求持久化集合属性字段必须声明为接口,实际的接口可以是java.util.Set,java.util.Collection,java.util.List,java.util.Map,java.util.SortedSet,java.util.SortedMap等,甚至是自己定义类型(只需要实现org.hibernate.usertype.UserCollectionType接口即可)。
Hibernate之所以要求用集合接口声明集合属性字段,是因为当程序持久某个实例时,Hibernate会自动把程序中的集合实现类替换成Hibernate自己的集合实现类,因此不要试图把Hibernate集合属性强制转换为集合实现类,如HashSet,HashMap等,但可以转换为Set,Map等集合,因为Hibernate自己的集合类也实现了Map,Set等接口。
集合映射的元素大致有如下这些:
list:用于映射List集合属性
set:用于映射Set集合属性
map:用于映射Map集合属性
array:用于映射数组集合属性
rimitive-array:专门用于映射基本数据类型的数组
bag:用于映射无须集合
idbag:用于映射无序集合,但为集合增加逻辑次序
List集合属性:Person.java和Person.hbm.xml
public class Person{ private String id; private String name; private List<String> schools; ...... }
<?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.test.domain"> <class name="Person" table="person"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" type="string" length="255" /> <list name="school" table="school"> <key column="person_id" not-null="true" /> <list-index column="list_order" /> <element type="string" column="school_name" /> </list> </class> </hibernate-mapping>
数组属性:Person.java和Person.hbm.xml
public class Person{ private String id; private String name; private String[] schools; ...... }
<?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.test.domain"> <class name="Person" table="person"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" type="string" length="255" /> <array name="school" table="school"> <key column="person_id" not-null="true" /> <list-index column="list_order" /> <element type="string" column="school_name" /> </array> </class> </hibernate-mapping>
set集合属性:Person.java和Person.hbm.xml
public class Person{ private String id; private String name; private Set<String> schools; ...... }
<?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.test.domain"> <class name="Person" table="person"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" type="string" length="255" /> <set name="schools" table="school"> <key column="person_id" not-null="true" /> <element type="string" column="school_name" not-null="true" /> </set> </class> </hibernate-mapping>
bag元素映射:Person.java和Person.hbm.xml
public class Person{ private String id; private String name; private Collection<String> schools; ...... }
<?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.test.domain"> <class name="Person" table="person"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" type="string" length="255" /> <bag name="schools" table="school"> <key column="person_id" not-null="true" /> <element type="string" column="school_name" not-null="true" /> </bag> </class> </hibernate-mapping>
Map集合属性:Person.java和Person.hbm.xml
public class Person{ private String id; private String name; private Map<String,Float> schools; ...... }
<?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.test.domain"> <class name="Person" table="person"> <id name="id" column="id" type="integer"> <generator class="native" /> </id> <property name="name" type="string" length="255" /> <map name="schools" table="school"> <key column="person_id" not-null="true" /> <map-key column="subject" type="string" /> <element column="grade" not-null="float" /> </map> </class> </hibernate-mapping>