<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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 閱讀(2238) 評論(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
    謝謝樓主的分享!  回復  更多評論
      
    主站蜘蛛池模板: 中文字幕亚洲天堂| 免费夜色污私人影院在线观看| 亚洲精品乱码久久久久久蜜桃不卡 | 亚洲国产精品毛片av不卡在线| 亚洲av日韩av永久无码电影| 成年女人免费视频播放体验区| 亚洲综合成人婷婷五月网址| 成年美女黄网站18禁免费| 色偷偷女男人的天堂亚洲网 | 色屁屁www影院免费观看视频| 好爽…又高潮了毛片免费看 | 97在线免费观看视频| 亚洲乱码国产乱码精品精| 亚洲免费观看视频| 久久国产亚洲高清观看| 69视频免费在线观看| 亚洲网站免费观看| 毛片a级三毛片免费播放| 久久亚洲精品11p| 亚洲人成网站观看在线播放| 成人免费一区二区三区| 久久亚洲日韩看片无码| 免费无码又爽又刺激聊天APP| 国产精品亚洲专区无码不卡| 亚洲午夜精品第一区二区8050| 嫩草在线视频www免费看| 久久亚洲私人国产精品| 女人让男人免费桶爽30分钟| 色多多www视频在线观看免费| 亚洲大尺度无码无码专区| 精品国产污污免费网站aⅴ| 亚洲精品V天堂中文字幕| 亚洲色欲色欲www在线丝| 亚洲免费中文字幕| 黄色免费在线观看网址| 久久噜噜噜久久亚洲va久| 黄瓜视频高清在线看免费下载| 日本黄页网址在线看免费不卡| 亚洲av鲁丝一区二区三区| 免费无码又爽又刺激高潮的视频 | 亚洲免费观看视频|