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& ...
随机推荐
- python基础课程_学习笔记26:编程的乐趣
编程的乐趣 编程柔术 当你坐下来,打算如何组织计划要定时,具体程序,然而,无论什么经验.在实现时间的函数的,你会逐渐学会了原来的设计,实用的新知识.我们不应该忽视沿途汲取的教训,相反,它们用于其他设计 ...
- C#内存分配学习
CLR内存分配分三大块区域:栈.GC堆.大对象堆. 一.线程堆栈(栈) 用于分配值类型实例.栈由操作系统进行管理,不受GC管理,当值类型不在其作用域(主要是指其所在函数内)时,其所占栈空间自动释放.栈 ...
- table在 点击线条颜色
效果图: <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- net开源cms系统
.net开源cms系统推荐 内容目录: 提起开源cms,大家第一想到的是php的cms,因为php开源的最早,也最为用户和站长们认可,随着各大cms系统的功能的不断完善和各式各样的开源cms的出现,. ...
- 5次Shift会触发粘滞键的妙用(转)
1.前提 你可以在平时亲身接触状态电脑,哪怕是在电脑主人不在的时候(虽然主人不在,或者关机了,进入电脑是要密码的). 2.原理 利用电脑连续按5次Shift会触发粘滞键,它会运行c:\winows\s ...
- hibernate 双向n-n
领域模型: 关系数据模型 双向 n-n 关联须要两端都使用集合属性 双向n-n关联必须使用连接表 集合属性应添加 key 子元素用以映射外键列, 集合元素里还应添加many-to-many子元素关联实 ...
- msys2 安装注意事项
它一直在使用 msys.有一个最近发现 msys2.而且msys2 配套的编译器是MinGW-w64. 就试着用了用,感觉还不错,这里把安装过程记录一下. 简单的说,MSYS2 是MSYS的一个升级版 ...
- POJ 2996 & 2993 国际象棋布局 模拟
Description Your task is to read a picture of a chessboard position and print it in the chess notati ...
- The maximum string content length quota (8192) has been exceeded while reading XML data
原文:The maximum string content length quota (8192) has been exceeded while reading XML data 问题场景:在我们W ...
- deepinmind(转)
http://it.deepinmind.com/ 花名有孚,支付宝工程师 有希望加入支付宝的同学,可以把简历发到我的个人邮箱spidercoco@gmail.com