`
lijunabc
  • 浏览: 47389 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hibernate list集合映射

阅读更多
Person.java
package com.aabnn.vo;

import java.util.ArrayList;
import java.util.List;

public class Person {
	private int id;
	private String name;
	private List<String> schools= new ArrayList<String>();
	public Person(){
		
	}
	public Person(String name,List<String> schools){
		this.name=name;
		this.schools=schools;
	}
	public int getId() {
		return id;
	}
	@SuppressWarnings(value="unused")
	private void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<String> getSchools() {
		return schools;
	}
	public void setSchools(List<String> schools) {
		this.schools = schools;
	}
	
}


Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.aabnn.vo">
  <class name="Person" table="person">
  	<id name="id" column="id" type="integer">
  		<generator class="identity" />
  	</id>
  	<property name="name" type="string" column="name" />
  	<list name="schools" table="schools" >
  		<key column="id" />
  		<list-index column="listindex" />
  		<element type="string" column="name" />
  	</list>
  	
  </class>
</hibernate-mapping>

Test.java
package com.aabnn.test;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import com.aabnn.vo.Person;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SessionFactory sf=new Configuration().configure().buildSessionFactory();
		Session sess=sf.openSession();
		Transaction tx=sess.beginTransaction();
		List<String> schools=new ArrayList<String>();
		schools.add("elementary school");
		schools.add("middle school");
		schools.add("college");
		Person p=new Person("xiaoxiao",schools);
		sess.save(p);
		tx.commit();
		sess.close();
	}

}

mysql 数据库
CREATE DATABASE IF NOT EXISTS collectionmapping;
USE collectionmapping;

--
-- Definition of table `person`
--

DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(45) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `person`
--

/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` (`id`,`name`) VALUES 
 (2,'xiaoxiao'),
 (3,'xiaoxiao');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;


--
-- Definition of table `schools`
--

DROP TABLE IF EXISTS `schools`;
CREATE TABLE `schools` (
  `id` int(10) unsigned NOT NULL,
  `name` varchar(45) NOT NULL default '',
  `listindex` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `schools`
--

/*!40000 ALTER TABLE `schools` DISABLE KEYS */;
INSERT INTO `schools` (`id`,`name`,`listindex`) VALUES 
 (2,'college',2),
 (2,'elementary school',0),
 (2,'middle school',1),
 (3,'college',2),
 (3,'elementary school',0),
 (3,'middle school',1);
分享到:
评论

相关推荐

    Hibernate常见集合映射(Set,List_Array,Map,Bag)

    Hibernate常见集合映射(Set,List_Array,Map,Bag)

    Hibernate映射集合属性List

    Hibernate映射集合属性List,用于映射集合属性 List Hibernate 自动生成表 不需sql

    hibernate集合的映射

    hibernate集合的映射 集合的映射 set list array map

    hibernate 集合映射

    适合初学者了解在hibernate中配置set,list,map,bag等集合映射

    Hibernate+中文文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

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

     2.4.3 Hibernate映射类型接口  2.4.4 可供扩展的接口  2.5 小结  2.6 思考题 第3章 第一个Hibernate应用  3.1 创建Hibernate的配置文件  3.2 创建持久化类  3.3 创建数据库Schema  3.4 创建对象-关系映射...

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

     6.3.1 基本的集合映射   6.3.2 排序集合和有序集合   6.3.3 映射嵌入式对象的集合   6.4 映射父/子关系   6.4.1 多样性   6.4.2 最简单的可能关联   6.4.3 使关联双向   6.4.4 级联对象状态   6.5...

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

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    HibernateAPI中文版.chm

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    Hibernate中文详细学习文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    hibernate 教程

    映射集合(Mapping a Collection) 6.3. 值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. 一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy ...

    Hibernate 中文 html 帮助文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    最全Hibernate 参考文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联(Ternary associations) 6.3.4. 使用 6.4. 集合例子...

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

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    Hibernate教程

    7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联(Ternary associations) 7.3.4. 使用 7.4. 集合...

    hibernate 体系结构与配置 参考文档(html)

    高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

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

     2.4.3 Hibernate映射类型接口  2.4.4 可供扩展的接口  2.5 小结  2.6 思考题 第3章 第一个Hibernate应用  3.1 创建Hibernate的配置文件  3.2 创建持久化类  3.3 创建数据库Schema  3.4 创建对象-关系映射...

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

     2.4.3 Hibernate映射类型接口  2.4.4 可供扩展的接口  2.5 小结  2.6 思考题 第3章 第一个Hibernate应用  3.1 创建Hibernate的配置文件  3.2 创建持久化类  3.3 创建数据库Schema  3.4 创建对象-关系映射...

Global site tag (gtag.js) - Google Analytics