集合类型映射
- Set 无序 元素不可重复
-
1 public class StudentSet { 2 private int id; 3 private String name; 4 private Set<String> images = new HashSet<String>();
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.base.model"> 7 8 <class name="StudentSet" table="t_studentSet"> <!-- 类名与对应的数据表名 --> 9 <id name="id" column="stuId"> <!-- 字段名,设置主键 --> 10 <generator class="native"/> 11 </id> 12 <property name="name" column="stuName"/> <!--普通字段 --> 13 14 <set name="images" table="t_image"> <!-- 新建了一张表 --> 15 <key column="studentId"></key> 16 <element column="imageName" type="string"></element> 17 </set> 18 </class> 19 20 </hibernate-mapping>
-
- List 有序 元素可重复
- 相对于set 多了一个 list-index 列, 可以寻找
-
1 <list name="images" table="t_imageList"> <!-- 新建了一张表 --> 2 <key column="studentId"></key> //外键 3 <list-index column="imageIndex"></list-index> //主键 4 <element column="imageName" type="string"></element> 5 </list>
- Bag 无序 元素可重复
- 使用List属性 ibag标签
-
1 <idbag name="images" table="t_images"> 2 <collection-id type="int" column="imagesId"> //主键 3 <generator class="increment"/> //生成策略 4 </collection-id> 5 <key column="studentId"></key> //外键 6 <element column="imageName" type="string"></element> 7 </idbag>
- Map 键值对
- 联合主键
-
1 <map name="students" table="student"> 2 <key column="team_id"></key> 3 <map-key column="name" type="java.lang.String"></map-key> 4 <element column="description" type="string"></element> 5 </map> 6