博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HIbernate Y映射 Map、list、set
阅读量:4322 次
发布时间:2019-06-06

本文共 5725 字,大约阅读时间需要 19 分钟。

1.总

  • set:不允许重复、无序
  • list:有序、重复
  • map:key-value对形式

2.map与set标签中的element子标签映射的是原子类型(string、data、int、long...),即就是能够直接映射到数据库表字段上的类型,而one-to-many映射的则是实体类型,指的是无法映射到表的某个字段,而是要映射到整张表的类型。

3.map

(1)原子类型

public class Team{    private String id;    private String name;    private Map students = new HashMap();}
public static void main(String[] args)    {        //生成表        SchemaExport export = new SchemaExport(new Configuration().configure());        export.create(true,true);    }
Team team = new Team();                team.setName("team");                    Map map = team.getStudents();        map.put("zhangsan","sdas");        map.put("lisi","sadasd");                Session session = sf.openSession();        Transaction ts = null;        try        {            ts = session.beginTransaction();            session.save(team);            ts.commit();        }        catch(Exception ex)        {            if(null != ts)            {                ts.rollback();            }        }        finally        {            session.close();        }

(2)实体类型

public class Team{    private String id;    private String name;    private Map students = new HashMap();}
public class Student{    private String id;    private String name;    private String cardId;    private int age;    private Team team;}
public static void main(String[] args)    {        Team team = new Team();        team.setName("team1");            Student student = new Student();        student.setAge(12);        student.setName("liule");            student.setTeam(team);                team.getStudents().put("123456",student);                Session session = sf.openSession();        Transaction ts = null;        try        {            session.save(team);            ts = session.beginTransaction();            ts.commit();        }        catch(Exception ex)        {            ex.printStackTrace();            if(null != ts)            {                ts.rollback();            }        }        finally        {            session.close();        }    }

4.set

public class Team{    private String id;    private String name;    private Set students = new HashSet();}
public static void main(String[] args)    {        Team team = new Team();        team.setName("team1");                team.getStudents().add("zhangsan");                        Session session = sf.openSession();        Transaction ts = null;        try        {            session.save(team);            ts = session.beginTransaction();            ts.commit();        }        catch(Exception ex)        {            ex.printStackTrace();            if(null != ts)            {                ts.rollback();            }        }        finally        {            session.close();        }    }

5.list

public class Team{    private String id;    private String name;    private List students = new ArrayList();}
public class Student{    private String id;    private String name;    private String cardId;    private int age;    private Team team;
public static void main(String[] args)    {        Team team = new Team();        team.setName("team1");                Student student1 = new Student();        Student student2 = new Student();        Student student3 = new Student();                student1.setAge(10);        student1.setCardId("asd");        student1.setName("zhangsan");        student1.setTeam(team);                student2.setAge(11);        student2.setCardId("asdsa");        student2.setName("lisi");        student2.setTeam(team);                student3.setAge(12);        student3.setCardId("asasad");        student3.setName("wangwu");        student3.setTeam(team);                team.getStudents().add(student1);        team.getStudents().add(student2);        team.getStudents().add(student3);                            Session session = sf.openSession();        session.save(team);        Transaction ts = null;        try        {            session.save(team);            ts = session.beginTransaction();            ts.commit();        }        catch(Exception ex)        {            ex.printStackTrace();            if(null != ts)            {                ts.rollback();            }        }        finally        {            session.close();        }    }

 

转载于:https://www.cnblogs.com/liu-Gray/p/4999439.html

你可能感兴趣的文章
PHP Curl发送数据
查看>>
HTTP协议
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Laravel 的生命周期
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
sessionStorage
查看>>
学习进度
查看>>
github.com加速节点
查看>>