博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
one2many &&many2many
阅读量:4964 次
发布时间:2019-06-12

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

只记录双向的情况(双向是单向的一种)

 @OneToMany 和 @ManyToOne :一个Group 包含多个 User;

Group.class

package com.XX.model;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name = "t_group")public class Group {    private String id;    private String name;    private String title;    private Set
users;   //说明代码一 @Id @GeneratedValue(generator = "sys-uuid") @GenericGenerator(name = "sys-uuid", strategy = "uuid2") public String getId() { return id; } public String getName() { return name; } public String getTitle() { return title; }  //说明代码二 @OneToMany(cascade = {CascadeType.ALL}, fetch=FetchType.EAGER, mappedBy = "group") public Set
getUsers() { return (users==null)? new HashSet
():users; } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setTitle(String title) { this.title = title; } public void setUsers(Set
users) { this.users = users; }   //说明代码三/* public void addUser(User user) { if(this.users == null){ this.users = new HashSet
(); } users.add(user); }*/}

User.class

package com.XX.model;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name = "t_user")public class User {    private String id;    private String name;    private int age;    private Group group;    @Id    @GeneratedValue(generator = "sys-uuid")    @GenericGenerator(name = "sys-uuid", strategy = "uuid2")    public String getId() {        return id;    }    public String getName() {        return name;    }        public int getAge() {        return age;    }    //说明代码二    @ManyToOne(cascade = {CascadeType.ALL})    public Group getGroup() {        return group;    }    public void setId(String id) {        this.id = id;    }    public void setName(String name) {        this.name = name;    }    public void setAge(int age) {        this.age = age;    }    public void setGroup(Group group) {        this.group = group;    }    }

测试代码: 

package com.XX.model;import java.util.Set;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.Metadata;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;public class GroupTest {        private static SessionFactory sessionFactory;        @BeforeClass    public static void beforeClass(){     //hibernate 5.0.0.Final及之后的写法        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();                Metadata metadata = new MetadataSources(registry).getMetadataBuilder().build();        sessionFactory = metadata.getSessionFactoryBuilder().build();    }    @Test    public void test() {        Group group = new Group();        group.setName("group1");        group.setTitle("test");                User user = new User();        user.setName("yuc");        user.setAge(0);                Set
users = group.getUsers(); users.add(user); group.setUsers(users);     //说明代码三 //group.addUser(user); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.save(group); session.getTransaction().commit();      //说明代码四 session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.get(Group.class, "1"); } @AfterClass public static void afterClass(){ sessionFactory.close(); } }

  

说明代码一:

  ID用 String 存储,保存时自动生成,跨数据库(mysql、oracle均实用);

说明代码二:

  @manytoone & @onetomany 双向关联时,manytoone 一方用mappedBy 进行关联(否则数据库会出现两个关联关系,重复);  

 

代码说明三:

  在多的一方辅助增加一个子元素。不足:破坏bean 的完整性(一般bean层级只有元素和对应的get/set方法);

代码说明四:

  用于说明@manytoone 和 @onetomany 的默认加载方式:

  @manytoone 默认加载方式是FetchType.LAZY;

  @onetomany 默认加载方式是FetchType.EAGER;

 

运行代码如下:  

 

数据库结果如下:  

 

@manyToMany双向关联  :一个Teacher有多个Student,一个Student有多个Teacher;

Teacher.class

package com.ajs.model;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToMany;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name = "t_teacher")public class Teacher {	private String id;	private String name;	private String title;	private Set
students; @Id @GeneratedValue(generator = "sys-uuid") @GenericGenerator(name = "sys-uuid", strategy = "uuid2") public String getId() { return id; } //说明代码五 @ManyToMany(targetEntity=Student.class, fetch = FetchType.EAGER, cascade={CascadeType.ALL}, mappedBy = "teachers") public Set
getStudents() { return students; } public void setStudents(Set
students) { this.students = students; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; }}

  

Student.class 

 

测试代码  

  @Test    public void test() {        Teacher teacher = new Teacher();        teacher.setName("tea1");        teacher.setTitle("test");                Student student = new Student();        student.setName("yuc");        student.setAge(20);                Set
students= new HashSet
(); students.add(student); teacher.setStudents(students); Set
teachers= new HashSet
(); teachers.add(teacher); student.setTeachers(teachers); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.save(teacher); session.getTransaction().commit(); session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.get(Student.class, "1"); }

  

说明代码五:

package com.ajs.model;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name = "t_student")public class Student {	private String id;	private String name;	private int age;	private Set
teachers; @Id @GeneratedValue(generator = "sys-uuid") @GenericGenerator(name = "sys-uuid", strategy = "uuid2") public String getId() { return id; } public String getName() { return name; } public int getAge() { return age; } //说明代码五 @ManyToMany(targetEntity=Teacher.class, fetch = FetchType.EAGER, cascade={CascadeType.ALL}) @JoinTable(name="t_teacher_student", joinColumns=@JoinColumn(name="student_id"), inverseJoinColumns=@JoinColumn(name="teacher_id")) public Set
getTeachers() { return teachers; } public void setId(String id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setTeachers(Set
teachers) { this.teachers = teachers; }}

  

  双向一样用 mappedBy 对其中一方进行限制,fetch = FetchType.EAGER手动将加载方式改为急加载,需要用targetEntity指定对应的关联类是哪一个;

  @JoinTable(name="t_teacher_student", joinColumns=@JoinColumn(name="student_id"),inverseJoinColumns=@JoinColumn(name="teacher_id"))

  @JoinTable可以自定义对应的中间表名字,当前实体对象关联的中间表字段名称,已经中间表关联另一方的对应名称;

运行代码如下:

数据库结果如下:

 

转载于:https://www.cnblogs.com/xx0405/p/5378301.html

你可能感兴趣的文章
设计模式_Java_工厂方法模式的概述和使用
查看>>
3Sum
查看>>
sql between and
查看>>
java 判断String 是否为空
查看>>
《剑指offer》第三十九题(数组中出现次数超过一半的数字)
查看>>
idea中加载配置文件找不到的问题解决办法
查看>>
jersey中的405错误 method not allowed
查看>>
nginx+nodejs
查看>>
Spring事务源码梳理
查看>>
plsql连接oracle10g报错:ora-12514错误的解决
查看>>
win8环境下安装sql2008r2
查看>>
要想有什么样的成就就要有什么样的眼光-SNF快速开发平台
查看>>
浅谈 HTML5 的 DOM Storage 机制 (转)
查看>>
[Leetcode] Linked List Cycle II
查看>>
Java语言:JAVA8 十大新特性详解(zz)
查看>>
线程上下文类加载器本质剖析与实现
查看>>
Bear and Blocks (dp+思维)
查看>>
jQuery源码分析第一章---匿名函数
查看>>
高精度求A*B(FFT)
查看>>
hdu 6298 Maximum Multiple(规律)
查看>>