Hibernat之关系的处理多对多
第一步:编写两个pojo,比如一个学生表一个课程表 这里使用注解。
需要
课程表:
package com.qcf.pox; 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; @Entity
public class Course {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@ManyToMany(mappedBy="courses")
private Set<Student2> student2s=new HashSet<Student2>();
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<Student2> getStudent2s() {
return student2s;
}
public void setStudent2s(Set<Student2> student2s) {
this.student2s = student2s;
}
public Course(int id, String name, Set<Student2> student2s) {
super();
this.id = id;
this.name = name;
this.student2s = student2s;
}
public Course() {
super();
} }
学生表:
package com.qcf.pox; import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany; @Entity
public class Student2 {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@ManyToMany(cascade=CascadeType.ALL)
private Set<Course> courses=new HashSet<Course>();
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<Course> getCourses() {
return courses;
}
public void addCourses(Course courses) {
this.courses.add(courses);
}
public Student2(int id, String name, Set<Course> courses) {
super();
this.id = id;
this.name = name;
this.courses = courses;
}
public Student2() {
super();
} }
第二步:在hibernate.cfg.xml文件中引入这两个po类
<mapping class="com.qcf.pox.Student2"/>
<mapping class="com.qcf.pox.Course"/>
第三步:编写测试代码
package com.qcf.test; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration; import com.qcf.pox.Course;
import com.qcf.pox.Student2; public class TestManyToMany {
public static void main(String[] args) {
Configuration configuration=new AnnotationConfiguration().configure();
SessionFactory factory=configuration.buildSessionFactory();
Session session=factory.openSession();
Transaction transaction=session.beginTransaction(); //创建两个课程
Course course=new Course();
course.setName("java");
Course course2=new Course();
course2.setName("C#"); //创建两个学生
Student2 student=new Student2();
student.setName("zhangsan");
Student2 student2=new Student2();
student2.setName("lisi"); student.addCourses(course);
student.addCourses(course2); student2.addCourses(course);
student2.addCourses(course2); session.save(student);
session.save(student2); transaction.commit();
session.close(); } }
执行成功后的结果:



Hibernat之关系的处理多对多的更多相关文章
- 关系/对象映射 多对多关系(@ManyToMany 注释)【重新认识】
old: @ManyToMany 注释:表示此类是多对多关系的一边, mappedBy 属性定义了此类为双向关系的维护端, 注意:mappedBy 属性的值为此关系的另一端的属性名. 例如,在Stud ...
- MyBatis加强(1)~myBatis对象关系映射(多对一关系、一对多关系)、延迟/懒加载
一.myBatis对象关系映射(多对一关系.一对多关系) 1.多对一关系: ---例子:多个员工同属于一个部门. (1)myBatis发送 额外SQL: ■ 案例:员工表通过 dept_id 关联 部 ...
- Hibernat之关系的处理一对多/多对一
第一步:编写两个pojo,比如一个学生表一个班级表 这里使用注解. 需要 班级表: package com.qcf.pox; import java.util.HashSet; import jav ...
- hibernate对象关系实现(三)多对多实现
单向n-n:(catogory-item)一个类别对应多个条目,一个条目对应多个类别 a.以类别类中有条目的集合的引用为例: b.数据库中的体现:建立一个新表,以类别和条目的主键关联的外键做新表的联合 ...
- HIbernate学习笔记(六) 关系映射之多对多
六.多对多 - 单向 Ø 一般的设计中,多对多关联映射,需要一个中间表 Ø Hibernate会自动生成中间表 Ø Hibernate使用many-to-ma ...
- Hibernate映射关系之_多对多
多对多关系由于效率的原因,实际中会拆成相互的一对多的关系,不再累述
- Hibernate表关系映射之多对多映射
一.多对多的实现原理 在数据库中实现多对多的关系,必须使用连接表.也就是用一个独立的表来存入两个表的主键字段,通过遍历这张表来获取两表的关联关系. 而在我们的对象中,多对多是通过两者对象类中互相建立对 ...
- Django框架表关系外键-多对多外键(增删改查)-正反向的概率-多表查询(子查询与联表查询)
目录 一:表关系外键 1.提前创建表关系 2.目前只剩 书籍表和 书籍作者表没创建信息. 3.增 4.删 5.修改 二:多对多外键增删改查 1.给书籍绑定作者 2.删 3.修改 4.清空 三:正反向的 ...
- 7.hibernat实现双向一对多(多对一)
1.创建如下项目结构 2.在项目的src下创建hibernate.cfg.xml主配置文件 <?xml version="1.0" encoding="UTF-8& ...
随机推荐
- HDOJ 4745 Two Rabbits DP
Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Tot ...
- Java彻底 - WEB容器的侦听具体解释 ServletContextListener
WEB容器的侦听器ServletContextListener主要用于监测容器启动和 当破坏需要做一些操作,听众将能够使用此做. ServletContextListener在Spring开始,然后再 ...
- Gradle多项目配置的一个demo
ParentProject├─build.gradle├─settings.gradle├─libs├─subProject1├────────────build.gradle├─────────── ...
- 本文摘录 - Infobright
背景 论文 Brighthouse: AnAnalytic Data Warehouse for Ad-hoc Queries.VLDB 2008 brighthouse它是一个面向列的数据仓库.在数 ...
- 解决LINUX vncserver 启动 could not open default font 'fixed'错.
安装vncserver例如,会发生以下错误: vncext: VNC extension running! vncext: Listening for VNC connecti ...
- c#左右socket连接超时控制方案
之前有一个项目中使用Remoting技术.当远程地址无效或server不执行,访问远程对象的方法,它会经过几十秒的时间来抛出异常秒. 由于我使用tcp状态.因此,认为可以使用socket为了测试连接, ...
- hdu 3911 Black And White(线段树)
题目连接:hdu 3911 Black And White 题目大意:给定一个序列,然后有M次操作: 0 l r:表示询问l,r中最大连续1的个数 1 l r:表示将l,r区间上的数取反 解题思路:线 ...
- 例如找出令人信服的权威C++中间malloc与new
例如找出令人信服的权威C++中间malloc与new 问题: 非常多人都知道malloc与new都是用来申请空间用的,开辟空间来源于堆中. 可是在C++中却非常少用malloc去申请空间,为什么? 以 ...
- OpenGL于MFC使用汇总(三)——离屏渲染
有时直接创建OpenGL形式不适合,或者干脆不同意然后创建一个表单,正如我现在这个项目,创建窗体不显示,它仅限于主框架.而我只是ActiveX里做一些相关工作,那仅仅能用到OpenGL的离屏渲染技术了 ...
- Oracle 11g+oracle客户端(32位)+PL/SQL develepment的安装配置
之前一直想学Oracle,可是就是安装配置Oracle一直未成功,让人很苦恼,特别是什么监听器什么的,一直没搞明白,弄了整整一天都没弄出来,上网查资料后发现资料上大多数都是参差不齐,不太详细明了,尝试 ...