024 其它 关联映射

其它映射包括如下:

  • Set:<set>标签映射

  并非一对多,多对一关联映射,此处的set中为普通数据类型,不是对象(如果是对象则是一对多,多对一)

n   Hibernate会使用一个单表来存储set元素,并加入一个外键参照主表记录,无序

  • List:<list>标签映射

n   Hibernate会使用一个单表来存储list元素,并加入一个外键参照主表记录,

n   因为list是有序的,所以hibernate需要加入一个索引字段,以保持存储顺序

  • Array:<array>标签映射

n   Hibernate会使用一个单表来存储array(数组)元素,并加入一个外键参照主表记录,

n   因为array(数组)也是有序的,所以hibernate也需要加入一个索引字段,以保持存储顺序

  • Map:<map>标签映射

n   Hibernate会使用一个单表来存储array(数组)元素,并加入一个外键参照主表记录,

n  因为map中有key和value,所以这两个字段是必需的。加上一个外键,共三个字段

n  因为Map是无序的,所以不用考虑顺序

实例类

如下:

public class CollectionMapping {

    private int id;
    private String name;   
    private Set setValue;  
    private List listVale; 
    private String[] arrayValue;   
    private Map mapValue;

    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 Set getSetValue() {
       return setValue;
    }

    public void setSetValue(Set setValue) {
        this.setValue = setValue;
    }

    public List getListVale() {
        return listVale;
    }

    public void setListVale(List listVale) {
        this.listVale = listVale;
    }

    public String[] getArrayValue() {
        return arrayValue;
    }

    public void setArrayValue(String[] arrayValue) {
        this.arrayValue = arrayValue;
    }

    public Map getMapValue() {
        return mapValue;
    }

    public void setMapValue(Map mapValue) {
        this.mapValue = mapValue;
    }
}

Hibernate对集合的存储关系:

t_collectionMapping

Id

Name

1

Xxxx

对于set的存储(无序)

Set_id(是一个外键参照上面的主表id)

Set_value

1

A

1

B

对于list的存在(有序:存入时需要存入一个索引以保持顺序)

List_id(是一个外键参照上面的主表id)

List_value

List_index(用于保持顺序)

1

C

0

1

D

1

对于array(数组)的存在(有序:存入时需要存入一个索引以保持顺序)

array_id(是一个外键参照上面的主表id)

array_value

array_index(用于保持顺序)

1

E

0

1

F

1

对于Map的存储(无序)

Map_id(是一个外键参照上面的主表id)

Map_key(Map中的Key)

Map_value(Map中的value)

1

K1

V1

1

K2

V2

集合关联映射文件实例:

<hibernate-mapping>

    <class name="com.wjt276.hibernate.CollectionMapping" table="t_collectionMapping">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name" column="name"/>
--------set--------------------------------------------------------------------------

        <!-- 使用<set>标签来映射Set类型,Set类型会映射到数据库中成为一个表
            name属性:对象属性名称
            table属性:重新指定表名
            再使用<key>标签在表映射一个外键参照<class>标签映射的表的主键
        -->

        <set name="setValue" table="t_set_value">
            <key column="set_id"/>
            <!-- 使用<element>标签来映射一个字段,用于存储Set中的元素,元素是普通类型
                type属性:指定Set存储元素的hiberante数据类型,也可以是Java包装类
                column属性:指定字段名       
            如果元素是对象则使用<composite-element>标签映射
                class属性:指定对象的完整包名
                column属性:指定字段名
                 -->
            <element type="string" column="set_value"/>
        </set>

 

--------List--------------------------------------------------------------------------

        <!-- 使用<list>标签来映射list类型,list类型会映射到数据库中成为一个表
            name属性:对象属性名称
            table属性:重新指定表名

            再使用<key>标签在表映射一个外键参照<class>标签映射的表的主键
            因为list是有序的,所以需要使用<list-index>标签加入一个索引字段,以保持存储顺序
        -->

        <list name="listValue" table="t_list_value">
            <key column="list_id"/>
            <list-index column="lint_index"/>
            <!-- 使用<element>标签来映射一个字段,用于存储list中的元素,元素是普通类型
                type属性:指定list存储元素的hiberante数据类型,也可以是Java包装类
                column属性:指定字段名       
            如果元素是对象则使用<composite-element>标签映射
                class属性:指定对象的完整包名
                column属性:指定字段名
                 -->
            <element type="string" column="list_value"/>
        </list>
--------Array-------------------------------------------------------------------
        <!-- 使用<array>标签来映射array(数组)类型,array(数组)类型会映射到数据库中成为一个表
            name属性:对象属性名称
            table属性:重新指定表名
            再使用<key>标签在表映射一个外键参照<class>标签映射的表的主键
            因为array(数组)是有序的,所以需要使用<list-index>标签加入一个索引字段,以保持存储顺序
        -->
        <array name="arrayValue" table="t_array_value">
            <key column="array_id"/>
            <list-index column="array_index"/>
            <!-- 使用<element>标签来映射一个字段,用于存储array(数组)中的元素,元素是普通类型
                type属性:指定array(数组)存储元素的hiberante数据类型,也可以是Java包装类
                column属性:指定字段名       
            如果元素是对象则使用<composite-element>标签映射
                class属性:指定对象的完整包名
                column属性:指定字段名
                 -->
            <element type="string" column="array_value"></element>
        </array>
--------Map-------------------------------------------------------------------

        <!-- 使用<map>标签来映射Map类型,Map类型会映射到数据库中成为一个表
            name属性:对象属性名称
            table属性:重新指定表名
            再使用<key>标签在表映射一个外键参照<class>标签映射的表的主键
            因为Map中有Key和Value是对应的,所以
            使用<map-key>来映射Map中的key
            而使用<element>或是<composite-element>映射Value
        -->
        <map name="mapValue" table="t_map_value">
            <key column="map_id"/>
            <map-key type="string" column="map_key"/>
            <!-- 使用<element>标签来映射一个字段,用于存储Map中value的元素,元素是普通类型
                type属性:指定Map中的Value存储元素的hiberante数据类型,也可以是Jva包装类
                column属性:指定字段名       
            如果元素是对象则使用<composite-element>标签映射
                class属性:指定对象的完整包名
                column属性:指定字段名
                 -->
            <element type="string" column="map_value"/>
        </map>
    </class>
</hibernate-mapping>

导出至数据库输出SQL语句:

create table t_collectionMapping (id integer not null auto_increment, name varchar(255), primary key (id))//创建主表

create table t_array_value (array_id integer not null, array_value varchar(255), array_index integer not null, primary key (array_id, array_index))//创建array存储表

create table t_list_value (list_id integer not null, list_value varchar(255), lint_index integer not null, primary key (list_id, lint_index))//创建List存储表

create table t_map_value (map_id integer not null, map_value varchar(255), map_key varchar(255) not null, primary key (map_id, map_key))//创建map存储表

create table t_set_value (set_id integer not null, set_value varchar(255),primary key。。。)//创建set存储表,主键呢?

alter table t_array_value add index FK2E0DD0C0BE7323E4 (array_id), add constraint FK2E0DD0C0BE7323E4 foreign key (array_id) references t_collectionMapping (id)

//array表的外键参照主表主键关系

alter table t_list_value add index FKE7AD7B3B165B837F (list_id), add constraint FKE7AD7B3B165B837F foreign key (list_id) references t_collectionMapping (id)

alter table t_map_value add index FK69DA1EC3CB0E6E01 (map_id), add constraint FK69DA1EC3CB0E6E01 foreign key (map_id) references t_collectionMapping (id)

alter table t_set_value add index FK56925949D585B13B (set_id), add constraint FK56925949D585B13B foreign key (set_id) references t_collectionMapping (id)

数据库表结构:

集合映射数据存储:

public void testSave1() {

        Session session = null;
        Transaction tr = null;

        CollectionMapping c = new CollectionMapping();
        c.setName("XXXX");
        // Set
        Set<String> setValue = new HashSet<String>();
        setValue.add("a");
        setValue.add("b");
        c.setSetValue(setValue);
        // List
        List<String> listValue = new ArrayList<String>();
       listValue.add("c");
        listValue.add("d");
        c.setListValue(listValue);
        // Array
        String[] arrayValue = new String[] { "e", "f" };
        c.setArrayValue(arrayValue);
        // Map
        Map<String, String> mapValue = new HashMap<String, String>();
        mapValue.put("k1", "v1");
        mapValue.put("k2", "v2");
        c.setMapValue(mapValue);
        try {
            session = HibernateUtils.getSession();
            tr = session.beginTransaction();
            session.save(c);
            tr.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            tr.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }

执行输出SQL语句:

Hibernate: insert into t_collectionMapping (name) values (?)

Hibernate: insert into t_set_value (set_id, set_value) values (?, ?)

Hibernate: insert into t_set_value (set_id, set_value) values (?, ?)

Hibernate: insert into t_list_value (list_id, lint_index, list_value) values (?, ?, ?)

Hibernate: insert into t_list_value (list_id, lint_index, list_value) values (?, ?, ?)

Hibernate: insert into t_array_value (array_id, array_index, array_value) values (?, ?, ?)

Hibernate: insert into t_array_value (array_id, array_index, array_value) values (?, ?, ?)

Hibernate: insert into t_map_value (map_id, map_key, map_value) values (?, ?, ?)

Hibernate: insert into t_map_value (map_id, map_key, map_value) values (?, ?, ?)

注意:首先插入一条主记录,然后再插入相应的集合数据到各个表中。

数据库中的存储结构

数据加载:

session = HibernateUtils.getSession();
        tr = session.beginTransaction();
        CollectionMapping c = (CollectionMapping)session.load(CollectionMapping.class, 1);
         
        System.out.println("name=" + c.getName());
        System.out.println("setValue=" + c.getSetValue());
        System.out.println("mapValue=" + c.getMapValue());
        System.out.println("arrayValue=" + c.getArrayValue());
        System.out.println("listValue=" + c.getListValue());
        tr.commit();
原文地址:https://www.cnblogs.com/crazylqy/p/4080592.html