多对多关联xml配置测试
2015-12-20 19:16
621 查看
Student<------>Course:
1.Student.hbm.xml:
2.Course.hbm.xml:
☆☆☆ 注意:inverse属性:只能有一个设置为true。也不能都不设置,否则会报异常(
1、Duplicate entry'2-2' for key 'PRIMARY';
2、Could notsynchronize database state with session;
2、ConstrainViolationException:Could not execute JDBC batch update;)
3.Test测试类:
4.控制台输出:
Hibernate: insert into Course (course_name) values (?)
Hibernate: insert into Course (course_name) values (?)
Hibernate: insert into Student (stu_name) values (?)
Hibernate: insert into Student (stu_name) values (?)
Hibernate: insert into stu_course (course_id, stu_id) values (?, ?)
Hibernate: insert into stu_course (course_id, stu_id) values (?, ?)
Hibernate: insert into stu_course (course_id, stu_id) values (?, ?)
Hibernate: insert into stu_course (course_id, stu_id) values (?, ?)
1.Student.hbm.xml:
<hibernate-mapping package="com.buaa.hibernate.bean"> <class name="Student"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="sname" column="stu_name"></property> <set name="courses" table="stu_course" inverse="true"> <key column="stu_id"></key> <many-to-many class="Course" column="course_id"></many-to-many> </set> </class> </hibernate-mapping>
2.Course.hbm.xml:
<hibernate-mapping package="com.buaa.hibernate.bean"> <class name="Course"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="cname" column="course_name"></property> <set name="students" table="stu_course"> <key column="course_id"></key> <many-to-many class="Student" column="stu_id"></many-to-many> </set> </class> </hibernate-mapping>
☆☆☆ 注意:inverse属性:只能有一个设置为true。也不能都不设置,否则会报异常(
1、Duplicate entry'2-2' for key 'PRIMARY';
2、Could notsynchronize database state with session;
2、ConstrainViolationException:Could not execute JDBC batch update;)
3.Test测试类:
public class Test extends TestCase { public void testMTM(){ //MySessionFactory是自己写的一个类,获取session Session session = MySessionFactory.getSession(0); Student stu = new Student(); stu.setSname("jack"); Student stu2 = new Student(); stu2.setSname("simon"); Course course = new Course(); course.setCname("English"); Course course2 = new Course(); course2.setCname("Math"); Set<Course> courses = new HashSet<Course>(); courses.add(course); courses.add(course2); Set<Student> students = new HashSet<Student>(); students.add(stu); students.add(stu2); stu.setCourses(courses); stu2.setCourses(courses); course.setStudents(students); course2.setStudents(students); session.save(course); session.save(course2); session.save(stu); session.save(stu2); session.beginTransaction().commit(); session.close(); } }
4.控制台输出:
Hibernate: insert into Course (course_name) values (?)
Hibernate: insert into Course (course_name) values (?)
Hibernate: insert into Student (stu_name) values (?)
Hibernate: insert into Student (stu_name) values (?)
Hibernate: insert into stu_course (course_id, stu_id) values (?, ?)
Hibernate: insert into stu_course (course_id, stu_id) values (?, ?)
Hibernate: insert into stu_course (course_id, stu_id) values (?, ?)
Hibernate: insert into stu_course (course_id, stu_id) values (?, ?)
相关文章推荐
- ubuntu下安裝sogou拼音
- find命令总结
- ThinkPHP连接mysql数据库
- CentOS 7系统中恢复Windows 7启动项
- 为什么Java中1000==1000为false而100==100为true?(Integer)
- kafka学习笔记
- Powershell-Lync:以OU为单位启用Lync账户
- Nginx 安装
- Java多线程编程中Future模式的详解
- 【Kylin】Kylin中的cube构建
- CSS水平、垂直居中小结
- Shell插入排序(c语言版)
- 指针数组,也称字符串数组
- 第1章 使用视图UI组件
- http://www.phplo.com/special/2013/0616/467.html
- Boosting算法简介
- Rod cutting - Using Dynamic Programming
- android studio 新建assets 文件
- 记录我第一次在Android开发图像处理算法的经历
- MIT 6.828 JOS学习笔记0. 写在前面的话