`
starbhhc
  • 浏览: 635180 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

hibernate bag 集合映射

阅读更多
Person.java

package com.aabnn.vo;   
  
import java.util.ArrayList;   
import java.util.Collection;   
  
public class Person {   
    private int id;   
    private String name;   
    private Collection<String> schools=new ArrayList<String>();   
    public Person(){}   
    public Person(String name,Collection<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 Collection<String> getSchools() {   
        return schools;   
    }   
    public void setSchools(Collection<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" column="name" type="string" />  
    <bag name="schools" table="schools">  
        <key column="id" />  
        <element column="name" type="string"/>  
    </bag>  
  </class>  
</hibernate-mapping>  



Test.java

package com.aabnn.test;   
  
import java.util.ArrayList;   
import java.util.Collection;   
  
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();   
        Collection<String> schools=new ArrayList<String>();   
        schools.add("xiaoxue");   
        schools.add("zhongxue");   
        schools.add("daxue");   
        Person p=new Person("xiaoxiao",schools);   
        sess.save(p);   
        tx.commit();   
        sess.close();   
    }   
  
}   
  



mysql 数据库

-- Create schema collectionmapping   
--   
  
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=2 DEFAULT CHARSET=utf8;   
  
--   
-- Dumping data for table `person`   
--   
  
/*!40000 ALTER TABLE `person` DISABLE KEYS */;   
INSERT INTO `person` (`id`,`name`) VALUES    
(1,'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) default NULL  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;   
  
--   
-- Dumping data for table `schools`   
--   
  
/*!40000 ALTER TABLE `schools` DISABLE KEYS */;   
INSERT INTO `schools` (`id`,`name`) VALUES    
(1,'xiaoxue'),   
(1,'zhongxue'),   
(1,'daxue');   
/*!40000 ALTER TABLE `schools` ENABLE KEYS */;   
分享到:
评论

相关推荐

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

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

    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

     14.2 映射Bag(包)  14.3 映射List(列表)  14.4 映射Map  14.5 对集合排序  14.5.1 在数据库中对集合排序  14.5.2 在内存中对集合排序  14.6 映射组件类型集合  14.7 小结  14.8 思考题 第15章 映射...

    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 中文 html 帮助文档

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

    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 参考文档

    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 教程

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

    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

     14.2 映射Bag(包)  14.3 映射List(列表)  14.4 映射Map  14.5 对集合排序  14.5.1 在数据库中对集合排序  14.5.2 在内存中对集合排序  14.6 映射组件类型集合  14.7 小结  14.8 思考题 第15章 映射...

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

     14.2 映射Bag(包)  14.3 映射List(列表)  14.4 映射Map  14.5 对集合排序  14.5.1 在数据库中对集合排序  14.5.2 在内存中对集合排序  14.6 映射组件类型集合  14.7 小结  14.8 思考题 第15章 映射...

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

     14.2 映射Bag(包)  14.3 映射List(列表)  14.4 映射Map  14.5 对集合排序  14.5.1 在数据库中对集合排序  14.5.2 在内存中对集合排序  14.6 映射组件类型集合  14.7 小结  14.8 思考题 第15章 映射...

    Hibernate中文API大全

    类似这样的映射允许你将一个many-to-many关联表映射为组合元素的集合。(A mapping like this allows you to map extra columns of a many-to-many association table to the composite element class.) 接下来的的...

Global site tag (gtag.js) - Google Analytics