隨筆-61  評論-159  文章-0  trackbacks-0
          hibernate中的集合的使用還是比較多,跟普通的string、int等字段處理的方式不一樣,下面介紹set、list、map、array集合在hibernate中的應用。

          例子:
          1、CollectionMapping的POJO類

           1import java.util.List;
           2import java.util.Map;
           3import java.util.Set;
           4
           5public class CollectionMapping {
           6    private int id;
           7    
           8    private String name;
           9    
          10    private Set setValue;
          11    
          12    private List listValue;
          13    
          14    private String[] arrayValue;
          15    
          16    private Map mapValue;
          17//省略setter、getter方法
          18}

          2、CollectionMapping.hbm.xml映射文件

           1<?xml version="1.0"?>
           2<!DOCTYPE hibernate-mapping PUBLIC 
           3    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
           4    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
           5<hibernate-mapping package="org.apple.hibernate">
           6    <class name="CollectionMapping" table="t_collectionMapping">
           7        <id name="id">
           8            <generator class="native"/>
           9        </id>
          10        <property name="name"/>
          11        <set name="setValue" table="t_set_value">
          12            <key column="set_id"/>
          13            <element type="string" column="set_value"/>
          14        </set>
          15        <list name="listValue" table="t_list_value">
          16            <key column="list_id"/>
          17            <list-index column="list_index"/>
          18            <element type="string" column="list_value"/>
          19        </list>
          20        <array name="arrayValue" table="t_array_value">
          21            <key column="array_id"/>
          22            <list-index column="array_index"/>
          23            <element type="string" column="array_value"/>
          24        </array>
          25        <map name="mapValue" table="t_map_value">
          26            <key column="map_id"/>
          27            <map-key type="string" column="map_key"/>
          28            <element type="string" column="map_value"/>
          29        </map>
          30    </class>
          31</hibernate-mapping>

          3、利用SchemaExport生成數據庫表

          mysql> desc t_array_value;
          +-------------+--------------+------+-----+---------+-------+
          | Field       | Type         | Null | Key | Default | Extra |
          +-------------+--------------+------+-----+---------+-------+
          | array_id    | int(11)      | NO   | PRI |         |       |
          | array_value | varchar(255) | YES  |     | NULL    |       |
          | array_index | int(11)      | NO   | PRI |         |       |
          +-------------+--------------+------+-----+---------+-------+
          3 rows in set (0.03 sec)


          mysql> desc t_array_value;
          +-------------+--------------+------+-----+---------+-------+
          | Field       | Type         | Null | Key | Default | Extra |
          +-------------+--------------+------+-----+---------+-------+
          | array_id    | int(11)      | NO   | PRI |         |       |
          | array_value | varchar(255) | YES  |     | NULL    |       |
          | array_index | int(11)      | NO   | PRI |         |       |
          +-------------+--------------+------+-----+---------+-------+
          3 rows in set (0.03 sec)


          mysql> desc t_list_value;
          +------------+--------------+------+-----+---------+-------+
          | Field      | Type         | Null | Key | Default | Extra |
          +------------+--------------+------+-----+---------+-------+
          | list_id    | int(11)      | NO   | PRI |         |       |
          | list_value | varchar(255) | YES  |     | NULL    |       |
          | list_index | int(11)      | NO   | PRI |         |       |
          +------------+--------------+------+-----+---------+-------+
          3 rows in set (0.03 sec)

          mysql> desc t_map_value;
          +-----------+--------------+------+-----+---------+-------+
          | Field     | Type         | Null | Key | Default | Extra |
          +-----------+--------------+------+-----+---------+-------+
          | map_id      | int(11)      | NO   | PRI |         |       |
          | map_value | varchar(255) | YES  |     | NULL    |       |
          | map_key   | varchar(255) | NO   | PRI |         |       |
          +-----------+--------------+------+-----+---------+-------+
          3 rows in set (0.03 sec)

          mysql> desc t_set_value;
          +-----------+--------------+------+-----+---------+-------+
          | Field     | Type         | Null | Key | Default | Extra |
          +-----------+--------------+------+-----+---------+-------+
          | set_id    | int(11)      | NO   | MUL |         |       |
          | set_value | varchar(255) | YES  |     | NULL    |       |
          +-----------+--------------+------+-----+---------+-------+
          2 rows in set (0.03 sec)


          4、測試方法:
           1public void testSave1()
           2    {
           3        Session session = null;
           4        CollectionMapping c = new CollectionMapping();
           5        
           6        c.setName("xxx");
           7        
           8        Set setValue = new HashSet();
           9        setValue.add("a");
          10        setValue.add("b");
          11        c.setSetValue(setValue);
          12        
          13        List listValue = new ArrayList();
          14        listValue.add("c");
          15        listValue.add("d");
          16        c.setListValue(listValue);
          17        
          18        String[] arrayValue = new String[]{"e""f"};
          19        c.setArrayValue(arrayValue);
          20        
          21        Map mapValue = new HashMap();
          22        mapValue.put("k1""v1");
          23        mapValue.put("k2""v2");
          24        c.setMapValue(mapValue);        
          25        try {
          26            session = HibernateUtil.getSession();
          27            session.beginTransaction();
          28            session.save(c);
          29            session.getTransaction().commit();
          30            
          31        }
           catch (Exception e) {
          32            // TODO: handle exception
          33            session.beginTransaction().rollback();
          34        }
          finally{
          35            session.close();
          36        }

          37    }




          -------------------------------------------------------------------------------------------------
          PS:本博客文章,如果沒有注明是有“轉”字樣,屬于本人原創。如果需要轉載,務必注明作者文章的詳細出處地址,否則不允許轉載,多謝合作!
          posted on 2008-10-26 13:16 apple0668 閱讀(2246) 評論(2)  編輯  收藏 所屬分類: hibernate

          評論:
          # re: 系統學習hibernate之十一:set、list、map、array集合 2008-10-26 23:09 | wt
          大哥!非常有用!謝謝嘍!!!!  回復  更多評論
            
          # re: 系統學習hibernate之十一:set、list、map、array集合 2012-08-04 11:32 | aderkayy
          謝謝樓主的分享!  回復  更多評論
            
          主站蜘蛛池模板: 新蔡县| 昌黎县| 沙湾县| 商水县| 卫辉市| 义乌市| 康定县| 东丰县| 公安县| 普宁市| 井陉县| 鸡东县| 界首市| 十堰市| 凌云县| 资中县| 邛崃市| 潮州市| 永安市| 鸡泽县| 庆阳市| 凤庆县| 石首市| 北辰区| 吉木乃县| 清新县| 翁牛特旗| 三江| 浠水县| 司法| 沁阳市| 彭水| 谷城县| 洛浦县| 临澧县| 剑川县| 耿马| 卢龙县| 武乡县| 沧州市| 玉环县|