• 自动秒收录
  • 软件:1974
  • 资讯:4527|
  • 收录网站:301505|

IT精英团

多对多关联映射

多对多关联映射

作者/景文

多对多关联映射

作者/景文

image.png

知识点:

多对多(teacher - student)
在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型;Hibernate会为我们创建中间关联表,转换成两个一对多。





照样实现步骤如下:
第一步:搭建hibernate运行所需的环境及*.jar 具体搭建见上面的博客
hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

truecom.mysql.jdbc.Driverjdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8com.mysql.jdbc.Driverrootliyongorg.hibernate.dialect.MySQLDialectupdate



第二步:编程实体类Student.java 和Teacher.java
Student.java
public class Student {
private int id;
private String name;
private Setteachers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SetgetTeachers() {
return teachers;
}
public void setTeachers(Setteachers) {
this.teachers = teachers;
}
}
Teacher.java
public class Teacher {
private int id;
private String name;
private Setstudents;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SetgetStudents() {
return students;
}
public void setStudents(Setstudents) {
this.students = students;
}
}
第三步:编写类的映射文件Student.hbm.xml、Teacher.hbm.xml
Teacher.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">






<!--下面是所映射的外键-->



Student.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">









第四步:编程测试
public class ManyToManyTest {
public static void main(String[] args) {
//add();
query(1);
}
@Test
public static void add() {
Session s = null;
Transaction tx = null;
try {
Setts = new HashSet();
Teacher t1 = new Teacher();
t1.setName("t1 name");
ts.add(t1);
Teacher t2 = new Teacher();
t2.setName("t2 name");
ts.add(t2);
Setss = new HashSet();
Student s1 = new Student();
s1.setName("s1");
ss.add(s1);
Student s2 = new Student();
s2.setName("s2");
ss.add(s2);
t1.setStudents(ss);
t2.setStudents(ss);
/* 不能同时学生和老师添加集合数据
s1.setTeachers(ts);
s2.setTeachers(ts);*/
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(t1);
s.save(t2);
s.save(s1);
s.save(s2);
tx.commit();
} finally {
if (s != null)
s.close();
}
}
@Test
public static void query(int id)
{
Session s = null;
try {
s=HibernateUtil.getSession();
Teacher teacher=(Teacher)s.get(Teacher.class, id);
Setstudents=teacher.getStudents();
System.out.println("teacher :"+teacher.getName());
for(Student student:students)
{
System.out.println("name_:"+student.getName());
}
}catch (Exception e) {
throw new RuntimeException("error");
}finally{
if(s!=null)
{
s.close();
}
}
}
}
第五:测试
源码见附件:

点击这里复制本文地址 以上内容由IT精英团整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
退出阅读|首页