package com.entity.manytomany;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany; import com.entity.BaseEntity;
@Entity
public class Student extends BaseEntity{
private String name;
@ManyToMany
//学生是不稳定的一方 所以由学生来维护
@JoinTable(name="student_course",
joinColumns=@JoinColumn(name="s_id"),
inverseJoinColumns=@JoinColumn(name="c_id"))
//关联表的表名
//关联当前表的主键
//关联对方表的主键 ()内的name即关联表内的字段名 可以自己自由设计
private List<Course> courses;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
} }

student.java

package com.entity.manytomany;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToMany; import com.entity.BaseEntity;
@Entity
public class Course extends BaseEntity{
private String name;
@ManyToMany(mappedBy ="courses")
//被学生类的courses对象维护
private List<Student> students;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
} }

Course.java

package com;

import java.util.ArrayList;
import java.util.List; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.entity.manytomany.Course;
import com.entity.manytomany.Student; public class TestManyToMany {
private Session s;
private Transaction tran;
@Before
public void before(){
s=HibernateUtils.getSession();
tran=s.beginTransaction(); } public void manytomany(){
Course c1=new Course();
Course c2=new Course();
Course c3=new Course();
Student stu1=new Student();
Student stu2=new Student();
Student stu3=new Student();
stu1.setName("zhangsan");
stu2.setName("lisi");
stu3.setName("wangwu");
c1.setName("java");
c2.setName("oracle");
c3.setName("html");
//新建对象
List<Course> courses =new ArrayList<Course>();//建立了一个课程集合
List<Course> courses1 =new ArrayList<Course>();
courses1.add(c2);
courses1.add(c3);
courses.add(c1);
courses.add(c2);
courses.add(c3);
//向集合中插入要选的课程
stu1.setCourses(courses);
stu2.setCourses(courses);
stu3.setCourses(courses1);
//建立关联 将课程集合set到学生的课程属性中 即学生选择了集合中所有的课程
s.save(c1);
s.save(c2);
s.save(c3);
s.save(stu1);
s.save(stu2);
s.save(stu3);
}
@Test
public void manytomanyquery(){
Course c=(Course)s.get(Course.class, 1);
for(Student s:c.getStudents()){
System.out.println(c.getName()+"-------选这门课的学生"+s.getName());
}
Course c1=(Course)s.get(Course.class, 2);
for(Student s:c1.getStudents()){
System.out.println(c1.getName()+"-------选这门课的学生"+s.getName());
}
Course c2=(Course)s.get(Course.class, 3);
for(Student s:c2.getStudents()){
System.out.println(c1.getName()+"-------选这门课的学生"+s.getName());
}
}
@After
public void after(){
tran.commit();
s.close();
}
}

testmanytomany.java

Hibernate 多对多映射的更多相关文章

  1. java框架篇---hibernate(多对多)映射关系

    以学生和老师为例的来讲解多对多映射. 实体类: Student package cn.itcast.g_hbm_manyToMany; import java.util.HashSet; import ...

  2. Hibernate学习(五)Hibernate 多对多映射

    说到多对多关系,印象最深刻的就是大学的选修课.一个学生可以选修多门课程,一门课程可以有多个学生选修,学生所选的每一门课程还有成绩.这个场景的E-R图如下: 对于多对多的关系,我们通常会抽出一张中间表( ...

  3. Hibernate多对多映射(双向关联)实例详解——真

    一个学生可以选多门课 一门课程有多个学生上 实现步骤: 一.学生 (1)数据库创建学生数据表students,包含id,name字段 设置id字段为主键,类型:bigint,自增 设置name字段,类 ...

  4. hibernate多对多映射文件详解(一)

    1.仓库表属性 public class WarehouseNew implements java.io.Serializable { // Fields private static final l ...

  5. hibernate多对多映射文件的配置

    user.hbm.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate ...

  6. hibernate多对多映射关系实现

    Course.hbm.xml: <?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        &q ...

  7. Hibernate 多对一映射

    <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBL ...

  8. hibernate多对多关联映射

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  9. hibernate笔记--单(双)向的多对多映射关系

    在讲单向的多对多的映射关系的案例时,我们假设我们有两张表,一张角色表Role,一张权限表Function,我们知道一个角色或者说一个用户,可能有多个操作权限,而一种操作权限同时被多个用户所拥有,假如我 ...

随机推荐

  1. PHP移动互联网开发(1)——环境搭建及配置

    原文地址:http://www.php100.com/html/php/rumen/2014/0326/6702.html 一.PHP5.4环境搭配基本流程 Apache:Web服务提供者.官网:ww ...

  2. JQuery的JSTree使用

    这是一个树形菜单的展示.其功能及其强大,几乎可以提供你对树结构的各种要求.下面,对其简述.    首先,感谢 Ivan Bozhanov利用JQuery对该组件的开发.同时还要感谢我的技术总监Mr. ...

  3. dict和set的使用

    使用dict和set dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子 ...

  4. BZOJ 1261: [SCOI2006]zh_tree( 区间dp )

    dp(l, r)表示[l, r]这段作为一棵树的最小访问代价. 对于dp(l, r), 我们枚举它的根x, 则dp(l, r) = min(dp(l, x-1)+dp(x+1, r)+C*fx) + ...

  5. html5之histroy浅析

    history是HTML5的新特性,我们可以使用它操作这个历史记录堆栈. (1)history提供了对浏览器历史纪录堆栈的读取,同时实现在访问记录中的前进和后退: history.length 历史记 ...

  6. asp.net 页面执行过程

    Application_BeginRequest Application_AuthenticateRequest Application_AuthorizeRequest Application_Re ...

  7. JS实现常用的分享到按钮

    我们阅读博客的时候经常会用到这样功能,当然有时候也会想把自己的网站上也加入类似的分享功能,各大厂商已经给出了相应的API,点击一个按钮即可弹出窗口进入分享,我们事先可以设置一些参数,一般常用的就是 网 ...

  8. stm32之595(spi芯片)

    595是一款串转并的芯片:  (三极管的功能) /*Include---------------------------*/ #include"stm32f10x_lib.h" / ...

  9. for循环语句之求和,阶乘,求偶,求n次篮球蹦起高度

    for循环语句格式: ;;/*循环条件*/i++/*状态改变*/) { //循环体,执行代码:(break;跳出循环体) } for 穷举法用循环把各种可能的情况都走一遍,然后用if条件把满足要求的结 ...

  10. ulipad双击无反应

    所有的东西都配好后,执行ulipad需要注意的是: 1,必须以管理员身份运行ulipad. 2,当运行有道词典的时候,双击ulipad是没有反应, 至于为什么会出现这种情况,我也不太清除,等我查到 原 ...