使用Hibernate处理集合元素
在使用Hibernate的过程中,需要对一个集合类型的元素操作。Hibernate支持List/Set/Map元素,在Hibernate的mapper中有List/Set/Bag/Map标签以支持这一特性。
向一个学生对象中插入多张照片
- Stduent.java
package com.java1234.hibernate.model;
import java.util.List;
public class Student {
private int id;
private String name;
private List<String> pictures;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getPictures() {
return pictures;
}
public void setPictures(List<String> pictures) {
this.pictures = pictures;
}
}
- 使用List - 插入的元素有序且允许重复
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.java1234.hibernate.model.Student" table="tb_student">
<id name="id" column="stuId">
<generator class="native"/>
</id>
<property name="name" column="stuName"/>
<list name="pictures" table="tb_picture">
<key column="stuId"/>
<list-index column="picIndex"/>
<element column="picName" type="string"/>
</list>
</class>
</hibernate-mapping>
生成的数据库表结构
Hibernate: create table tb_picture (stuId integer not null, picName varchar(255), picIndex integer not null, primary key (stuId, picIndex))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), primary key (stuId))
Hibernate: alter table tb_picture add constraint FK_dpi255h9ltoc1glix2hq8r2og foreign key (stuId) references tb_student (stuId)
使用Set - 元素不可重复且无序
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.java1234.hibernate.model.Student" table="tb_student">
<id name="id" column="stuId">
<generator class="native"/>
</id>
<property name="name" column="stuName"/>
<set name="pictures" table="tb_picture">
<key column="stuId"/>
<element column="picName" type="string"/>
</set>
</class>
</hibernate-mapping>
生成的数据库表结构
Hibernate: create table tb_picture (stuId integer not null, picName varchar(255))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), primary key (stuId))
Hibernate: alter table tb_picture add constraint FK_dpi255h9ltoc1glix2hq8r2og foreign key (stuId) references tb_student (stuId)
使用Bag - 元素无序/可以重复
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.java1234.hibernate.model.Student" table="tb_student">
<id name="id" column="stuId">
<generator class="native"/>
</id>
<property name="name" column="stuName"/>
<idbag name="pictures" table="tb_picture">
<collection-id type="integer" column="picId">
<generator class="increment"/>
</collection-id>
<key column="stuId"/>
<element column="picName" type="string"/>
</idbag>
</class>
</hibernate-mapping>
生成的数据库表结构
Hibernate: create table tb_picture (stuId integer not null, picName varchar(255), picId integer not null, primary key (picId))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), primary key (stuId))
Hibernate: alter table tb_picture add constraint FK_dpi255h9ltoc1glix2hq8r2og foreign key (stuId) references tb_student (stuId)
使用Map
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.java1234.hibernate.model.Student" table="tb_student">
<id name="id" column="stuId">
<generator class="native"/>
</id>
<property name="name" column="stuName"/>
<map name="pictures" table="tb_picture">
<key column="stuId"/>
<map-key column="picKey" type="string"/>
<element column="picName" type="string"/>
</map>
</class>
</hibernate-mapping>
生成的数据库表结构
Hibernate: create table tb_picture (stuId integer not null, picName varchar(255), picKey varchar(255) not null, primary key (stuId, picKey))
Hibernate: create table tb_student (stuId integer not null auto_increment, stuName varchar(255), primary key (stuId))
Hibernate: alter table tb_picture add constraint FK_dpi255h9ltoc1glix2hq8r2og foreign key (stuId) references tb_student (stuId)