zoukankan      html  css  js  c++  java
  • Hibernate 多对多映射

    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

  • 相关阅读:
    显示磁盘信息
    给VG增加磁盘,给文件目录增加空间
    第8.11节 Python类中记录实例变量属性的特殊变量__dict__
    Python中format_map与format字符串格式化的区别
    生成器函数与函数的区别
    什么是Python生成器?与迭代器的关系是什么?
    第8.10节 使用__class__查看Python中实例对应的类
    Python运算符的优先级是怎样的?
    Python怎么控制将一个整数输出成指定长的十六进制数?
    Python中函数的参数带星号是什么意思?
  • 原文地址:https://www.cnblogs.com/tianhao/p/4022866.html
Copyright © 2011-2022 走看看