`

[转]hibernate 集合类(Collections)映射(四)- map

阅读更多

四、Map集合映射

 

Map集合属性不仅需要映射属性value,还需要映射属性key。这里假设Employeename属性是唯一的,如下修改Employee.hbm.xml配置文件中的name属性,设置unique='true'

 

Java代码

<property name="name" unique="true"/> 

 

<property name="name" unique="true"/> 实体类Department如下:

 

Java代码

public class Department {  

 

    private int id;  

    private String name;  

    private  Map<String, Employee> emps;  

//settergetter方法  

 

} 

 

public class Department {

 

    private int id;

    private String name;

    private  Map<String, Employee> emps;

//settergetter方法

 

} 修改Department.hbm.xml配置文件如下:

 

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.reiyen.hibernate.domain"> 

    <class name="Department"> 

        <id name="id"> 

            <generator class="native" /> 

        </id> 

        <property name="name" /> 

        <map name="emps"> 

            <key column="depart_id" /> 

            <map-key type="string" column="name" /> 

            <one-to-many class="Employee" /> 

        </map> 

    </class> 

</hibernate-mapping> 

 

<?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.reiyen.hibernate.domain">

    <class name="Department">

       <id name="id">

           <generator class="native" />

       </id>

       <property name="name" />

       <map name="emps">

           <key column="depart_id" />

           <map-key type="string" column="name" />

           <one-to-many class="Employee" />

       </map>

    </class>

</hibernate-mapping> 将测试类下如下注释部分(即List部分)替换,改成Map重新进行测试:

 

Java代码

//List<Employee> list= new ArrayList<Employee>();  

//          list.add(employee2);  

//          list.add(employee1);  

            Map<String,Employee> emps = new HashMap<String,Employee>();  

            emps.put(employee1.getName(), employee1);  

            emps.put(employee2.getName(), employee2);  

            depart.setEmps(emps); 

 

//List<Employee> list= new ArrayList<Employee>();

//         list.add(employee2);

//         list.add(employee1);

           Map<String,Employee> emps = new HashMap<String,Employee>();

           emps.put(employee1.getName(), employee1);

           emps.put(employee2.getName(), employee2);

           depart.setEmps(emps); 测试结果如下:

 

emps:{employee1 name1 =id=1 name=employee1 name1, employee2 name2 =id=2 name=employee2 name2}(红色标记部分为key部分)

 

数据库表中记录如下所示(未发生变化):

 

mysql> select * from department;

+----+-----------------+

| id | name            |

+----+-----------------+

|  1 | department name |

+----+-----------------+

1 row in set (0.00 sec)

 

mysql> select * from employee;

+----+-----------------+-----------+

| id | name            | depart_id |

+----+-----------------+-----------+

|  1 | employee1 name1 |         1 |

|  2 | employee2 name2 |         1 |

+----+-----------------+-----------+

2 rows in set (0.00 sec)

分享到:
评论

相关推荐

    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_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. ...

    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中文详细学习文档

    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 中文 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 体系结构与配置 参考文档(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 参考文档

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

    7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.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中文API大全

    &lt;composite-map-key&gt;元素允许你映射一个组件类作为一个Map的key,前提是你必须正确的在这个类中重写了hashCode() 和 equals()方法。 8.4. 组件作为联合标识符(Components as composite identifiers) 你可以使用一...

    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.04中文文档.chm

    7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3. ...

    hibernate 框架详解

    7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.2.3...

    Hibernate3+中文参考文档

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

    7. 集合类(Collections)映射 7.1. 持久化集合类(Persistent collections) 7.2. 集合映射( Collection mappings ) 7.2.1. 集合外键(Collection foreign keys) 7.2.2. 集合元素(Collection elements) 7.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. ...

    NHibernate中文帮助手册API

    集合类(Collections)映射  6.1. 持久化集合类  6.2. 集合外键(Collection foreign keys)  6.3. 值集合于多对多关联(Collections of values and many-to-many associations)  6.4. 一对多关联  6.5. 延迟...

    NHibernate中文帮组文档(2008.11月更新)

    6. 集合类(Collections)映射 6.1. 持久化集合类 6.2. 集合外键(Collection foreign keys) 6.3. 值集合于多对多关联(Collections of values and many-to-many associations) 6.4. 一对多关联 6.5. 延迟加载 6.6. 有序...

    hibernate_reference中文文档.pdf

    1.2.1. 映射 Person 类 ................................................ 13 1.2.2. 单向 Set-based 的关联 ......................................... 14 1.2.3. 使关联工作 .....................................

Global site tag (gtag.js) - Google Analytics