hibernate学習シリーズ-----(9)hibernateの集合属性に対する操作のMap集合編

15202 ワード

やはり、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ではラベルを使用して構成できます.まずラベルの一般的な属性とサブ要素を理解しましょう.要素はjavaをマッピングするために使用されます.util.Mapタイプの属性で、よく使われる属性とサブ要素は次のとおりです.
  • nameプロパティ
  • table属性
  • サブエレメント
  • サブエレメント
  • サブエレメント
  • Javaでは、mapコレクションインタフェースにデータを格納するのはキー値ペアの形式を採用しているため、は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)を見てください.