Hibernate关联映射
2021-07-03 18:05
标签:format bsp source 1.0 ssi private table center close
1.单项多对一关联
首先创建实体类
public class Qx {
private int jdid;
private String jdname;
// Getters & Setters ...
}
public class Jd{
private int qxid;
private String qxname;
private Qx qx
// Getters & Setters ..;
}
配置文件,下文省略
/span>
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
2:双对多
public class Course {
private int courseid;
private String courseName;
private Set
public class Student {
//学生
private int studentid;
private String studentname;
private Set
br> "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
br> "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
测试类
package cn.yunhe.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.yuhe.entity.Course;
import cn.yuhe.entity.Student;
public class Demo {
Session session = null;
Transaction tr = null;
@Before
public void setUp() {
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
session = factory.openSession();
tr = session.beginTransaction();
}
@Test
public void manyTomany(){
Course course1 = new Course();
Course course2 = new Course();
course1.setCourseName("java");
course2.setCourseName("c");
session.save(course1);
session.save(course2);
Student stu1 = new Student();
Student stu2 = new Student();
stu1.setStudentName("小样");
stu2.setStudentName("集雨");
stu1.getCourses().add(course1);
stu1.getCourses().add(course2);
stu2.getCourses().add(course1);
stu2.getCourses().add(course2);
session.save(stu1);
session.save(stu2);
tr.commit();
}
@After
public void tearDown() {
session.close();
}
}
Hibernate关联映射
标签:format bsp source 1.0 ssi private table center close
原文地址:http://www.cnblogs.com/lxiaoyang/p/7123648.html