双向关联:多对多
来源:互联网 发布:手机音乐编辑软件 编辑:程序博客网 时间:2024/05/02 00:39
Teacher.java
package com.domain;import java.util.HashSet;import java.util.Set;public class Teacher {private Integer id;private String name;private Set<Student> students = new HashSet<Student>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}}
Student.java
package com.domain;import java.util.HashSet;import java.util.Set;public class Student {private Integer id;private String name;private Set<Teacher> teachers = new HashSet<Teacher>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set<Teacher> getTeachers() {return teachers;}public void setTeachers(Set<Teacher> teachers) {this.teachers = teachers;}}Teacher.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.domain"><class name="Teacher" table="teacher"><id name="id"><generator class="native"/></id><property name="name" column="name"/> <set name="students" table="teacher_student"> <key column="teacher_id"/> <many-to-many class="Student" column="student_id"/> </set></class></hibernate-mapping>
Student.hbm.xml
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate2</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.password">123456</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping resource="com/domain/Department.hbm.xml"/> <mapping resource="com/domain/Employee.hbm.xml"/> <mapping resource="com/domain/Teacher.hbm.xml"/> <mapping resource="com/domain/Student.hbm.xml"/> </session-factory></hibernate-configuration>测试类:
package com.junit;import org.hibernate.Session;import org.junit.Test;import com.domain.Student;import com.domain.Teacher;import com.utils.HibernateSessionFactory;public class ManyToMany {@Testpublic void add(){Teacher teacher1 = new Teacher();teacher1.setName("李老师");Teacher teacher2 = new Teacher();teacher2.setName("张老师");Student student1 = new Student();student1.setName("张三");student1.getTeachers().add(teacher1);student1.getTeachers().add(teacher2);Student student2 = new Student();student2.setName("李四");student2.getTeachers().add(teacher1);student2.getTeachers().add(teacher2);//teacher1.getStudents().add(student1);//teacher1.getStudents().add(student2);////teacher2.getStudents().add(student1);//teacher2.getStudents().add(student2);Session session = HibernateSessionFactory.getSession();session.beginTransaction();session.save(teacher1);session.save(teacher2);session.save(student1);session.save(student2);session.getTransaction().commit();HibernateSessionFactory.closeSession();}}
- 多对多双向关联
- 双向关联:多对多
- Hibernate多对多双向关联映射
- Hibernate 多对多双向关联
- hibernate 多对多 双向关联实战
- 双向多对多关联实例
- 多对多关联之双向
- 多对多关联映射 单向+双向
- JPA多对多双向关联实例
- Hibernate 多对多双向关联
- Hibernate 多对多双向关联
- Hibernate 多对多双向关联
- hibernate多对多双向关联_Annotation
- hibernate多对多双向关联_XML
- Hibernate多对多双向关联
- 双向关联:一对多/多对一
- JPA多对多双向关联
- Hibernate 多对多双向关联
- ajax默认是不能跨域通讯的
- VIM 快捷键
- 51单片机串口通信实例
- Bitbucket免费代码托管空间:可绑域名 私有Repos 个人空间
- win8 Dota全屏设置
- 双向关联:多对多
- play framework中实现分页
- (Manual) Interactive Data Visualization for the Web
- 机器学习实战Logistic回归之马儿得疝气了,怎么办?
- 【loading】Android常用代码集合
- 提示框的显示与消失
- cocos2d-helloworld
- 字符串专项:UVa 11855
- 关于socket长连接的心跳包