`

hibernate 集合映射

阅读更多

Hibernate要求持久化集合值字段必须声明为接口。

  • List:用于映射List集合属性
  • Set:用于映射Set集合属性
  • Map:用于映射Map集合性
  • Array:用于映射数组集合属性
  • Bag:用于映射无序集合
  • idbag:用于映射无序集合,但为集合增加逻辑次序
  •  

    映射如下:
      
       <set name="setValue" table="t_setvalue">
       <!--
    添加一字段指向id -->
        <key column="setid"/>
        <element type="string" column="setvalue"/>
       </set>

     

    +-----------+--------------+------+-----+---------+-------+
    | Field     | Type         | Null | Key | Default | Extra |
    +-----------+--------------+------+-----+---------+-------+
    | set_id    | int(11)      | NO   | MUL |         |       |
    | set_value | varchar(255) | YES  |     | NULL    |       |
    +-----------+--------------+------+-----+---------+-------+

      
       <list name="listValue" table="t_listvalue">
       <!--
    添加一字段指向
    id -->
        <key column="listid"/>
       <!--
    标记list中数据的顺序
    -->
        <list-index column="listindex"/>
        <element type="string" column="listvalue"/>
       </list>
    +------------+--------------+------+-----+---------+-------+
    | Field      | Type         | Null | Key | Default | Extra |
    +------------+--------------+------+-----+---------+-------+
    | list_id    | int(11)      | NO   | PRI |         |       |
    | list_value | varchar(255) | YES  |     | NULL    |       |
    | list_index | int(11)      | NO   | PRI |         |       |
    +------------+--------------+------+-----+---------+-------+


       <array name="arrayValue" table="t_array">
       <!--
    添加一字段指向id -->
        <key column="arrayid"/>
       <!--
    标记array中数据的顺序
    -->
        <list-index column="arrayindex"/>
        <element type="string" column="arrayvalue"/>
       </array>

    +-------------+--------------+------+-----+---------+-------+
    | Field       | Type         | Null | Key | Default | Extra |
    +-------------+--------------+------+-----+---------+-------+
    | array_id    | int(11)      | NO   | PRI |         |       |
    | array_value | varchar(255) | YES  |     | NULL    |       |
    | array_index | int(11)      | NO   | PRI |         |       |
    +-------------+--------------+------+-----+---------+-------+


       <map name="mapValue" table="t_map">
        <key column="mapid"/>
        <map-key type="string" column="mapkey"/>
        <element type="string" column="mapvalue"/>
       </map>

    +-----------+--------------+------+-----+---------+-------+
    | Field     | Type         | Null | Key | Default | Extra |
    +-----------+--------------+------+-----+---------+-------+
    | map_id      | int(11)      | NO   | PRI |         |       |
    | map_value | varchar(255) | YES  |     | NULL    |       |
    | map_key   | varchar(255) | NO   | PRI |         |       |
    +-----------+--------------+------+-----+---------+-------+

     

     

    bag与ibag元素映射,具体可参考http://www.ntsky.com/docs/java/HibernateGossip/HibernateGossip/Bag.html
    bag元素既可以为List集合属性映射,也可以为Collection集合属性映射。不管是哪种集合属性,使用bag元素都将被映射成无序集合,而集合属性对应的表没有
    xml 代码

    <bag name="school" table="schools">
    <key column="pid" not-null="true"/>
    <element type="string" column="school_name"/>
    <bag>

     

     

     

    其中:<element type="string" column="mapvalue"/>可用<one-to-many class=""/>、<many -to-oneclass=""/>、<many -to-many class=""/>、<composite-element class=""/>代替,原因是在多的一方加个外键关联。

    分享到:
    评论

    相关推荐

    Global site tag (gtag.js) - Google Analytics