Hibernate初学者---多对多单/双向关联
来源:互联网 发布:2k17加内特捏脸数据 编辑:程序博客网 时间:2024/06/05 16:02
Hibernate 版本: v5.2.10
Eclipse版本:v4.7.1
JDK版本: v1.8
MySQL版本:v5.7
多对多单向
POJO类一
package com.bak.bum.union;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.Table;@Entity@Table(name = "100_teacher")public class Teacher { private int id; private String name; Set<Students> students = new HashSet<Students>(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public int getId() { return id; } public String getName() { return name; } @ManyToMany @JoinTable(name = "tsTable", joinColumns = { @JoinColumn(name = "TeacherID") }, inverseJoinColumns = { @JoinColumn(name = "StudentsID") }) public Set<Students> getStudents() { return students; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setStudents(Set<Students> students) { this.students = students; }}
POJO类二
package com.bak.bum.union;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="100_students")public class Students { private int id; private String name; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public int getId() { return id; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; }}
Junit -TEST类
package com.bak.fan.test;import java.util.EnumSet;import org.hibernate.boot.Metadata;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.service.ServiceRegistry;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.hibernate.tool.schema.TargetType;public class Test { @org.junit.Test public void test() { ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata(); SchemaExport schemaExport = new SchemaExport(); schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);}}
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">shwythnn00</property> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name=""></property> <mapping class="com.bak.bum.union.Teacher" /> <mapping class="com.bak.bum.union.Students" /> </session-factory></hibernate-configuration>
输出结果
SHOW DB
多对多双向
POJO类一
package com.bak.bum.union;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.Table;@Entity@Table(name = "200_teacher")public class Teacher { private int id; private String name; Set<Students> students = new HashSet<Students>(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public int getId() { return id; } public String getName() { return name; } @ManyToMany @JoinTable(name = "tsTable", joinColumns = { @JoinColumn(name = "TeacherID") }, inverseJoinColumns = { @JoinColumn(name = "StudentsID") }) public Set<Students> getStudents() { return students; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setStudents(Set<Students> students) { this.students = students; }}
POJO类二
package com.bak.bum.union;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.ManyToMany;import javax.persistence.Table;@Entity@Table(name = "200_students")public class Students { private int id; private String name; private Set<Teacher> teacher = new HashSet<Teacher>(); @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public int getId() { return id; } public String getName() { return name; } @ManyToMany(mappedBy="students") public Set<Teacher> getTeacher() { return teacher; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setTeacher(Set<Teacher> teacher) { this.teacher = teacher; }}
Junit-Test类
package com.bak.fan.test;import java.util.EnumSet;import org.hibernate.boot.Metadata;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.service.ServiceRegistry;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.hibernate.tool.schema.TargetType;public class Test { @org.junit.Test public void test() { ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata(); SchemaExport schemaExport = new SchemaExport(); schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);}}
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">shwythnn00</property> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">create</property> <property name=""></property> <mapping class="com.bak.bum.union.Teacher" /> <mapping class="com.bak.bum.union.Students" /> </session-factory></hibernate-configuration>
输出结果
SHOW DB;
JAR
多对多单双向关联,对数据表无影响。只是在程序上可以互相访问。
阅读全文
0 0
- Hibernate初学者---多对多单/双向关联
- hibernate多对多关联--单双向
- Hibernate关联映射之多对多单/双向关联映射
- Hibernate初学者---多对一/一对多 双向关联
- Hibernate初学者---多对多双向关联数据表的CRUD增删改查
- Hibernate多对多双向关联映射
- Hibernate 多对多双向关联
- hibernate 多对多 双向关联实战
- Hibernate 多对多双向关联
- Hibernate 多对多双向关联
- Hibernate 多对多双向关联
- hibernate多对多双向关联_Annotation
- hibernate多对多双向关联_XML
- Hibernate多对多双向关联
- Hibernate 多对多双向关联
- Hibernate多对多双向关联
- hibernate 多对多双向关联
- hibernate多对多双向关联
- 双向链表
- openwrt ssh登陆第一次出来的字符修改
- 几个常见的链表面试题<一>
- 在多重继承里面出现的错误
- Crossing(一维单点更新,区间查询)
- Hibernate初学者---多对多单/双向关联
- 【java基础】解决Java中 while(Scanner.hasNext())一直为死循环的问题!
- 你需要知道的10位java开发牛人
- [agc016d]XOR Replace
- jQuery
- Scala学习1.2 Scala中Array Map等数据结构实战
- CentOS 7 安装教程
- 求数组中第二大的数
- 单片机PID调速控制直流无刷电机附部分源码