`

hibernate集合类映射

阅读更多

关系图

t_CollectionMapping

 

id

name

1

xxx

 

t_set_values

 

set_id

set_value

1

a

1

b

 

t_list_value

 

list_id

list_value

list_index

1

c

0

1

d

1

 

t_array_value

 

array_id

array_value

array_index

1

e

0

1

f

1

 

t_map_value

 

map_id

map_key

map_value

1

k1

v1

1

k2

v2

 

其中set、list、map、array的ID都指向CollectionMapping的id

 

1、实体文件

public class CollectionMapping {
	
	private int id;
	
	private String name;
	
	private Set setValue;
	
	private List listValue;
	
	private String[] arrayValue;
	
	private Map mapValue;
...

 

2、映射文件

<hibernate-mapping>
	<class name="com.bjsxt.hibernate.CollectionMapping" table="t_CollectionMapping">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="setValue" table="t_set_value">
			<key column="set_id"/>
			<element type="string" column="set_value"/>
		</set>
		<list name="listValue" table="t_list_value">
			<key column="list_id"/>
			<list-index column="list_index"/>
			<element type="string" column="list_value"/>
		</list>
		<array name="arrayValue" table="t_array_value">
			<key column="array_id"/>
			<list-index column="array_index"/>
			<element type="string" column="array_value"/>
		</array>
		<map name="mapValue" table="t_map_value">
			<key column="map_id"/>
			<map-key type="string" column="map_key"/>
			<element type="string" column="map_value"/>
		</map>
	</class>
</hibernate-mapping>

 

3、TestCase

public class CollectionMappintTest extends TestCase {

	public void testSave1() {
		Session session = null;
		CollectionMapping c = new CollectionMapping();
		
		c.setName("xxx");
		
		Set setValue = new HashSet();
		setValue.add("a");
		setValue.add("b");
		c.setSetValue(setValue);
		
		List listValue = new ArrayList();
		listValue.add("c");
		listValue.add("d");
		c.setListValue(listValue);
		
		String[] arrayValue = new String[]{"e", "f"};
		c.setArrayValue(arrayValue);
		
		Map mapValue = new HashMap();
		mapValue.put("k1", "v1");
		mapValue.put("k2", "v2");
		c.setMapValue(mapValue);		
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			session.save(c);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}	
	
	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			CollectionMapping c = (CollectionMapping)session.load(CollectionMapping.class, 1);
			System.out.println("name=" + c.getName());
			System.out.println("setvalue=" + c.getSetValue());
			System.out.println("mapvalue=" + c.getMapValue());
			System.out.println("listvalue=" + c.getListValue());
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}			
	
}

 

分享到:
评论

相关推荐

    hibernate_集合映射_set方法

    2,建立表的hibernate支持,生成tb_user类和对应的配置文件 3,修改配置文件使之和相应的类对应, 4,修改tb_user类,创建一个set类型的字段emailes,生成它的set和get方法 5,写测试类,对以上内容进行测试。 二、...

    Hibernate_code

    里面有所有hibernate 多种映射的代码:继承映射,实体映射,集合类映射等很全,适合初学者。

    hibernate pdf 电子书

    hibernate pdf 电子书 hibernate 入门 基本配置 持久化类 集合类 映射 事务和并发

    Java的Hibernate框架中集合类数据结构的映射编写教程

    Hibernate可以将Java中几个内置的集合结构映射为数据库使用的关系模型,下面我们就来看一下Java的Hibernate框架中集合类数据结构的映射编写教程:

    Hibernate+中文文档

    6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...

    Hibernate3.2官方中文参考手册

    Hibernate3.2官方中文参考手册,内容涉及Hibernate入门知识、配置、集合类映射、关联关系映射、组件、事务并发、批量处理、对象事件等知识。

    hibernate学习笔记

    每个具体类映射成一张表(hibernate_extends_3) 25 组件component映射(hibernate_component) 27 复合(联合)主键映射(hibernate_composite) 27 集合(collection)映像 (hibernate_collection) 28 Hibernate 对...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     3.4.2 把Customer持久化类映射到CUSTOMERS表  3.5 通过Hibernate API操纵数据库  3.5.1 Hibernate的初始化  3.5.2 访问Hibernate的Session接口  3.6 运行helloapp应用  3.6.1 创建运行本书范例的系统环境  ...

    hibernate3.2中文文档(chm格式)

    6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...

    HibernateAPI中文版.chm

    6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...

    hibernate中文参考文档

    6、集合类映射 7、组件映射 8、继承映射 9、操作持久化数据 10、事务和并行 11、Hibernate查询语言 12、条件查询 13、原生SQL查询 14、性能提升 15、工具箱指南 16、示例:父子关系 17、示例:Weblog应用程序 18、...

    Hibernate中文详细学习文档

    6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...

    hibernate 教程

    集合类(Collections)映射 6.1. 持久化集合类(Persistent Collections) 6.2. 映射集合(Mapping a Collection) 6.3. 值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. ...

    Hibernate 中文 html 帮助文档

    6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引...

    Hibernate参考指南

    1.Hiberante入门 2.体系结构 3.配置 4.持久化类 5.对象/关系数据库映射基础 6.集合类映射 7.关联关系映射 。。。

    Hibernate框架参考文档

    6. 集合类(Collections)映射; 7. 关联关系映射; 8. 组件(Component)映射; 9. 继承映射(Inheritance Mappings); 10. 与对象共事; 11. 事务和并发; 12. 拦截器与事件(Interceptors and events); 13. 批量处理(Batch...

    Hibernate实战(第2版 中文高清版)

     4.3 类映射选项   4.3.1 动态的SQL生成   4.3.2 使实体不可变   4.3.3 给查询命名实体   4.3.4 声明包名称   4.3.5 用引号把SQL标识符括起来   4.3.6 实现命名约定   4.4 细粒度的模型和映射   ...

    Hibernate Annotations 中文文档

    Hibernate Annotations API 中文文档 前言 1. 创建一个注解项目 1.1. 系统需求 1.2. 系统配置 2. 实体Bean 2.1. 简介 2.2. 用EJB3注解进行映射 2.2.1. 声明实体bean 2.2.1.1. 定义表(Table) 2.2.1.2. 乐观...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. ...

Global site tag (gtag.js) - Google Analytics