Hibernate 集合映射,适用于一对多的情况

来源:互联网 发布:北京百知尚行是培训吗 编辑:程序博客网 时间:2024/06/06 08:21


Hibernate里的集合映射其实也就是jdk里的集合框架的一个使用。下面会根据一个小的demo,来说明具体的使用。

首先给回顾一下,java的集合框架,下面的图是从网上找的,仅供参考。



一、Hibernate之Set集合映射:

1映射java里的java.util.Set接口,特点:

    a、元素存放没有顺序且不允许重复

    b、元素可以按自然顺序排列


二、Hibernate之List集合映射

1、映射java里的java.util.List接口。 特点:

   a、用一个额外的索引列保存每个元素的位置

三、Hibernate之Map集合映射

1、映射java里的java.util.Map接口。 特点:

   a、它素以键/值对的形式保存

   b、无序的

   c、如果映射类型为java.util.SortMap接口,则元素可以按自然顺序进行排序

下面直接贴代码:

首先是实体UserEntity

package com.fei.shop.Model;import javax.persistence.*;import javax.print.StreamPrintService;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;/** * Created by jing on 16/7/11. */@Entity@Table(name = "User", schema = "shop", catalog = "")public class UserEntity {    private int id;    private String name;    private int sex;    //map集合属性   喜欢的    private Map<Integer,String> mapLikes;    public Map<Integer, String> getMapLikes() {        return mapLikes;    }    public void setMapLikes(Map<Integer, String> mapLikes) {        this.mapLikes = mapLikes;    }    //list集合属性   喜欢的    private List<String> listLikes;    public List<String> getListLikes() {        return listLikes;    }    public void setListLikes(List<String> listLikes) {        this.listLikes = listLikes;    }    //定义Set集合属性   喜欢的    private Set<String> likes=new HashSet<String>();    public void setLikes(Set<String> likes) {        this.likes = likes;    }    public Set<String> getLikes() {        return likes;    }    @Id    @Basic    @GeneratedValue(strategy=GenerationType.IDENTITY)    @Column(name = "id", unique = true, nullable = false)    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    @Basic    @Column(name = "name", nullable = true, length = 200)    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Basic    @Column(name = "sex", nullable = true)    public int getSex() {        return sex;    }    public void setSex(int sex) {        this.sex = sex;    }    @Override    public boolean equals(Object o) {        if (this == o) return true;        if (o == null || getClass() != o.getClass()) return false;        UserEntity that = (UserEntity) o;        if (id != that.id) return false;        if (sex != that.sex) return false;        if (name != null ? !name.equals(that.name) : that.name != null) return false;        return true;    }    @Override    public int hashCode() {        int result = id;        result = 31 * result + (name != null ? name.hashCode() : 0);        result = 31 * result + sex;        return result;    }}
接着是  实体对应的映射文件 UserEntity.hbm.xml
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <class name="com.fei.shop.Model.UserEntity" table="User" schema="shop">        <id name="id" type="java.lang.Integer">            <generator class="native"></generator>        </id>        <property name="name">            <column name="name" sql-type="varchar(200)" length="200"/>        </property>        <property name="sex">            <column name="sex" sql-type="int(11)"/>        </property>        <!--set集合-->        <set name="likes" table="set_userLike">            <!--指定的外键id,是实体UserEntity的主键-->            <key><column name="id" not-null="true"></column></key>            <!--指定存储likes的字段类型-->            <element type="java.lang.String">                <column name="likes" not-null="true"></column>            </element>        </set>        <!--List集合-->        <list name="listLikes" table="list_Likes">            <!--指定的外键id,是实体UserEntity的主键-->            <key><column name="id" not-null="true"></column></key>            <!--额外的指定排序的字段-->            <list-index column="index_"></list-index>            <element type="java.lang.String">                <column name="listLikes" not-null="true"></column>            </element>        </list>        <!--Map 集合-->        <map name="mapLikes" table="map_likes">            <!--指定的外键id,是实体UserEntity的主键-->            <key><column name="id" not-null="true"></column></key>            <!--Map对应的key,指定每个元素对应的key-->            <map-key column="likeid" type="java.lang.Integer"></map-key>            <!--指定存储mapLikes的字段类型-->            <element type="java.lang.String">                <column name="mapLikes" not-null="true"></column>            </element>        </map>    </class></hibernate-mapping>


最后是调用方式,我是通过spring注解进行测试,就不一一展示了,只展示具体实现:

package com.fei.shop.Action;import com.fei.shop.Model.UserEntity;import com.fei.shop.Service.TestImpl;import com.fei.shop.Service.TestService;import jdk.nashorn.internal.ir.annotations.Reference;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;import java.util.*;/** * Created by jing on 16/7/11. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations= "classpath:SpringContext.xml")public class Test {    @Autowired    private TestService testService;    @org.junit.Test    public  void TestOrm()    {        UserEntity model=new UserEntity();        model.setName("测试126");        model.setSex(1);        //创建Set集合        Set<String> likes=new HashSet<String>();        likes.add("篮球");        likes.add("足球");        likes.add("排球");        //将Set集合赋值给User        model.setLikes(likes);        //list集合        List<String> list=new ArrayList<String>();        list.add("list篮球");        list.add("list足球");        list.add("list排球");        model.setListLikes(list);        //map集合        Map<Integer,String> map=new HashMap<Integer, String>();        map.put(1,"map篮球");        map.put(2,"map足球");        map.put(3,"map排球");        model.setMapLikes(map);        testService.save(model);    }}

现在把数据结果展示一下,小伙伴们看到数据就明白了:



 


0 0
原创粉丝点击