hibernate学习系列-----(9)hibernate对集合属性的操作之Map集合篇

照旧,先新建一个StudentMap.java实体类,将hobby属性使用map集合接口来存放:

package com.joe.entity;

import java.util.Map;

public class StudentMap {
    
    private int id;
    private String name;
    private int age;
    private Map<String,String> hobby;
    
    /**
     * 无参构造函数
     */
    public StudentMap(){
        
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Map<String, String> getHobby() {
        return hobby;
    }

    public void setHobby(Map<String, String> hobby) {
        this.hobby = hobby;
    }
    
    
    

}

接下来就是配置对象关系映射文件了,在hibernate中可以使用<map>标签来配置,先了解一下<map>标签的常见属性及子元素吧:<map>元素用来映射java.util.Map类型的属性,常用的属性和子元素有:

  1. name属性
  2. table属性
  3. <key>子元素
  4. <map-key>子元素
  5. <element>子元素

在java中,map集合接口存放数据是采用键值对的形式,因此<map-key>就是来map的键的,因此它也需要额外的列来保存键值。StudentMap.hbm.xml中的内容是:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <!-- 一个class标签对应一个实体类,name属性指定实体类名称,table属性指定关联的数据库表 -->
    <class name="com.joe.entity.StudentMap" table="stu_map_tab">
        <!-- 主键 -->
        <id name="id" column="stu_id">
            <!-- 提供ID自增的策略  native会根据数据库自行判断 -->
            <generator class="native"></generator>
        </id>
        <!-- 其他属性,name对应实体类的属性,column对应关系型数据库表的列 -->
        <property name="name" column="stu_name"></property>
        <property name="age" column="stu_age"></property>
        
        <map name="hobby" table="hobby_map_tab">
            <key column="student_id"></key>
            <map-key column="keycolumn" type="string"></map-key>
            <element type="string" column="hobby"></element>
        </map>
        
    </class>
</hibernate-mapping>

最后贴上StudentMapTest.java类的代码吧,这个测试和以前的几篇文章中的测试无异,就不在一个一个地展示了:

package com.joe.test;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import com.joe.entity.StudentMap;
import com.joe.util.HibernateUtils;

public class StudentMapTest {
    
    @Test
    public void createTable() {
        Configuration cfg = new Configuration().configure();
        SchemaExport se = new SchemaExport(cfg);
        se.create(true, true);
    }
    
    
    
    @Test
    public void save(){
        Transaction tx=null;
        Session session=null;
        try{
            session=HibernateUtils.getSession();
            tx=session.beginTransaction();
            
            StudentMap stu=new StudentMap();
            stu.setName("wangwu");
            stu.setAge(20);
            
            @SuppressWarnings({ "unchecked", "rawtypes" })
            Map<String,String> map=new HashMap();
            map.put("a", "run");
            map.put("b", "eat");
            
            stu.setHobby(map);
            
            session.save(stu);
            
            
            tx.commit();
        }catch(HibernateException he){
            if(tx!=null){
                tx.rollback();
            }
            he.printStackTrace();
        }finally{
            HibernateUtils.closeSession(session);
        }
    }
    
    @Test
    public void findAll(){
        Transaction tx=null;
        Session session=null;
        try{
            session=HibernateUtils.getSession();
            tx=session.beginTransaction();
            
            StudentMap stu=(StudentMap)session.get(StudentMap.class, 1);
            System.out.println(stu.getId()+stu.getName()+stu.getAge());
            
            Map<String,String > map=stu.getHobby();        
            System.out.println(map);
            tx.commit();
        }catch(HibernateException he){
            if(tx!=null){
                tx.rollback();
            }
            he.printStackTrace();
        }finally{
            HibernateUtils.closeSession(session);
        }
    }

}

hibernate对集合的操作就写到这儿了,其实还有很多没有写到的,如果有大神看过我的文章,希望多多指点,最后,关于java集合的学习,我在园子里看到有园友写过一个系列,感觉写得非常好,建议大家看看,Java 集合系列目录(Category)