标签:hibernate
多对多表的搭建
java类中
多对多
1、关系操作
1、多对多,谁操作效率都一样
2、解除关系
把第三张表的一行数据删除掉
3、建立关系
把第三张表的数据增加一行记录
4、变更关系
先删除后增加
2、级联操作
都是对象针对集合的操作
例子
工具类
public class HibernateUtils { public static SessionFactory sessionFactory; public static String url; @Before public void init(){ Configuration configuration = new Configuration(); if(url==null){ configuration.configure(); }else{ configuration.configure(url); } sessionFactory = configuration.buildSessionFactory(); }
student映射文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.itcast.many.Student"> <id name="sid" length="5"> <generator class="increment"></generator> </id> <property name="sname" length="20"></property> <property name="description" length="100"></property> <set name="coursess" table="student_course" cascade="save-update"> <key> <column name="sid"></column> </key> <many-to-many class="cn.itcast.many.Courses" column="cid"></many-to-many> </set> </class> </hibernate-mapping>
courses映射文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.itcast.many.Courses"> <id name="cid" length="5"> <generator class="increment"></generator> </id> <property name="cname" length="20"></property> <property name="description" length="100"></property> <set name="students" table="student_course" cascade="save-update"> <key> <column name="cid"></column> </key> <many-to-many class="cn.itcast.many.Student" column="sid"></many-to-many> </set> </class> </hibernate-mapping>
测试
@Test public void testCreateTable(){ } @Test public void testSaveStudent_Cascade_Courses_Save(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = new Student(); student.setSname("班长"); student.setDescription("牛人"); Courses Courses = new Courses(); Courses.setCname("生理卫生"); Courses.setDescription("很好"); Set<Courses> Coursess = new HashSet<Courses>(); Coursess.add(Courses); student.setCoursess(Coursess); session.save(student); transaction.commit(); session.close(); } /** * 已经存在一个课程,新建一个学生,建立课程和学生之间的关系 * 从课程角度出发 */ @Test public void testUpdateCourses_Cascade_Student_Save_R(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Courses Courses = (Courses)session.get(Courses.class, 1L); Student student = new Student(); student.setSname("班迷"); student.setDescription("班丝:班长的钢丝"); Courses.getStudents().add(student); session.save(student); transaction.commit(); session.close(); } /** * 已经存在一个课程,新建一个学生,建立课程和学生之间的关系 * 从学生角度出发 */ @Test public void testSaveStudent_R(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Courses Courses = (Courses)session.get(Courses.class, 1L); Student student = new Student(); student.setSname("班迷"); student.setDescription("班丝:班长的钢丝"); Set<Courses> Coursess = new HashSet<Courses>(); Coursess.add(Courses); student.setCoursess(Coursess); session.save(student); transaction.commit(); session.close(); } /** * 已经存在一个课程,已经存在一个学生,建立关联 */ @Test public void testR(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Courses Courses = (Courses)session.get(Courses.class, 1L); Student student = (Student)session.get(Student.class, 2L); student.getCoursess().add(Courses); transaction.commit(); session.close(); } /** * 把学生3,4加入到课程1中 */ @Test public void testR_Some(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Courses Courses = (Courses)session.get(Courses.class, 1L); Student student = (Student)session.get(Student.class, 3L); Student student2 = (Student)session.get(Student.class, 4L); //student.getCoursess().add(Courses); //student2.getCoursess().add(Courses); Courses.getStudents().add(student2); Courses.getStudents().add(student); transaction.commit(); session.close(); } /** * 把一个学生加入到一些课程中 */ @Test public void testR_Some_2(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = (Student)session.get(Student.class, 3L); List<Courses> CoursesList = session.createQuery("from Courses where cid in(1,2,3)").list(); student.getCoursess().addAll(CoursesList); transaction.commit(); session.close(); } /** * 把学生从一个课程转移到另外一个课程 */ @Test public void testTransform(){ Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student = (Student) session.get(Student.class, 3L); Courses Courses1 = (Courses)session.get(Courses.class, 1L); Courses Courses3 = (Courses)session.get(Courses.class, 3L); student.getCoursess().remove(Courses1); student.getCoursess().add(Courses3); transaction.commit(); session.close(); }
关联删除的错误
1、根据映射文件可以得出classes与student有关联
2、在客户端,students是由classes产生的,代表了关联关系
3、所以在删除student的时候,必须通过classes解除关系
4、解除关系以后才能进行删除
标签:hibernate
原文地址:http://pengya123.blog.51cto.com/8467424/1811628