hibernate单表,一对多,多对多配置文件详解

一、单表配置方式(使用配置关系映射文件去操作单表的CRUD操作)

1.新建Course.java

public class Course {
	
	private Integer id;  //课程id
	private String name; // 课程名称
	

	public Course() {
	}

	public Course(Integer id, String name) {
		this.id = id;
		this.name = name;
	}

	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 String toString() {
		return "Course [id=" + id + ", name=" + name + "]";
	}

}
2.新建Course.hbm.xml

<?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 package="com.iflytek.model">
	<class name="Course">
	    <!-- 第一种配置id方式 -->
		<id name="id" type="java.lang.Integer">
			<generator class="increment">
				<param name="increment">course_inc</param>
			</generator>
		</id>
		<!-- 第二种配置id方式 -->
		<!-- <id name="id" column="id" type="java.lang.Integer">
			<generator class="native">
			</generator>
		</id> -->
		<property name="name" type="java.lang.String">
			<column name="name" length="64" />
		</property>
	</class>
</hibernate-mapping>

3.测试类TestCourse.java

public class TestCourse {
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init(){
		//创建配置对象,加载配置文件 hibernate.cfg.xml
		Configuration configuration = new Configuration().configure();
		//创建会话连接工厂
		sessionFactory = configuration.buildSessionFactory();
		//打开会话
		session = sessionFactory.openSession();
		//开启事务
		transaction = session.beginTransaction();
	}
	

	@After
	public void destory(){
		//提交事务
		transaction.commit();
		//关闭会话
		session.close();
		//关闭工厂会话连接
		sessionFactory.close();
		
	}

	/**
	 * 新增课程信息
	 */
	@Test
	public void testSaveCourse() {
		Course cou = new Course();
		cou.setName("java");
		session.save(cou);
		
		Course cou2 = new Course(2, "python");
		session.save(cou2);
	}

	/**
	 * 更新课程信息
	 */
	@Test
	public void testUpdateStu() {
		// 获取一个课程对象
		Course cou = (Course) session.get(Course.class, 1);
		cou.setName("c++");
		session.save(cou);
	}

	/**
	 * 删除课程信息
	 */
	@Test
	public void testDeleteStu() {
		Course cou = (Course) session.get(Course.class, 2);
		session.delete(cou);
	}

	/**
	 * 查询课程信息
	 */
	@Test
	public void testGetStu() {
		Course cou = (Course) session.get(Course.class, 1);
		System.out.println(cou);

	}



}


二、一对多配置方式(一个学生对应多个选课记录)

1.新建Course3.java

public class Course3 {
	
	private Integer id;  //课程id
	private String name; // 课程名称
	
	private Student3 stu;

	public Course3() {
	}

	public Course3(Integer id, String name) {
		this.id = id;
		this.name = name;
	}

	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 Student3 getStu() {
		return stu;
	}

	public void setStu(Student3 stu) {
		this.stu = stu;
	}

	public String toString() {
		return "Course [id=" + id + ", name=" + name + "]";
	}

}
2.新建Course3.hbm.xml

<?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 package="com.iflytek.model3">
	<class name="Course3">
		<id name="id" type="java.lang.Integer">
			<generator class="increment">
				<param name="increment">course_inc</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="64" />
		</property>
		<!-- 配置多对一的关系 -->
		<many-to-one name="stu" column="sid"></many-to-one>
	</class>
</hibernate-mapping>

3.新建Student3.java

public class Student3 {

	private Integer sid; // 学生id
	private String name; // 学生姓名
	private String gender; // 学生姓名
	private Date birthday; // 学生出生日期
	
	private Set<Course3> cous = new HashSet<Course3>();

	public Student3() {

	}

	

	public Integer getSid() {
		return sid;
	}



	public void setSid(Integer sid) {
		this.sid = sid;
	}



	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}



	public String getGender() {
		return gender;
	}



	public void setGender(String gender) {
		this.gender = gender;
	}



	public Date getBirthday() {
		return birthday;
	}



	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}



	public Set<Course3> getCous() {
		return cous;
	}



	public void setCous(Set<Course3> cous) {
		this.cous = cous;
	}



	public String toString() {
		return "Student [sid=" + sid + ", name=" + name + ", gender=" + gender
				+ ", birthday=" + birthday + "]";
	}
	
}

4.新建Student3.hbm.xml
<?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 package="com.iflytek.model3">
	<class name="Student3">
		<id name="sid" type="integer">
			<generator class="increment">
				<param name="increment">stu_inc</param>
			</generator>
		</id>
		<property name="name" type="string">
			<column name="name" length="64" />
		</property>

		<property name="gender" type="string">
			<column name="gender"></column>
		</property>
		<property name="birthday" type="timestamp">
			<column name="birthday"></column>
		</property>
		
		<!-- 这里我们配置了one-to-many 一个学生可以对应多个选课记录 -->
		<set name="cous" cascade="save-update,delete">
			<key column="sid"></key>
			<one-to-many class="Course3" />
		</set>
	</class>
</hibernate-mapping>
5.测试类TestStudent3.java

public class TestStudent3 {
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init(){
		//创建配置对象,加载配置文件 hibernate.cfg.xml
		Configuration configuration = new Configuration().configure();
		//创建会话连接工厂
		sessionFactory = configuration.buildSessionFactory();
		//打开会话
		session = sessionFactory.openSession();
		//开启事务
		transaction = session.beginTransaction();
	}
	

	@After
	public void destory(){
		//提交事务
		transaction.commit();
		//关闭会话
		session.close();
		//关闭工厂会话连接
		sessionFactory.close();
		
	}

	/**
	 * 新增学生信息
	 */
	@Test
	public void testSaveStudent() {
		Student3 stu = new Student3();
		stu.setName("张三丰");
		stu.setGender("男");
		stu.setBirthday(new Date());
		
		Course3 cou1 = new Course3();
		cou1.setName("java");
		
		Course3 cou2 = new Course3();
		cou2.setName("c");
		
		stu.getCous().add(cou1);
		stu.getCous().add(cou2);
		
		
		session.save(stu);
	
	}

	/**
	 * 更新学生信息
	 */
	@Test
	public void testUpdateStu() {
		// 获取一个学生对象
		Student3 stu = (Student3) session.get(Student3.class, 1);
		stu.setName("张峰");
		session.save(stu);
	}

	/**
	 * 删除学生信息,同时删除课程表course3对应的数据
	 */
	@Test
	public void testDeleteStu() {
		Student3 stu = (Student3) session.get(Student3.class, 1);
		session.delete(stu);
	}

	/**
	 * 查询学生信息
	 */
	@Test
	public void testGetStu() {
		Student3 stu = (Student3) session.get(Student3.class, 1);
		System.out.println(stu);
	}
}


三、多对多(学生与课程多对多关系)

1.新建Course4.java

public class Course4 {
	
	private Integer id;  //课程id
	private String name; // 课程名称
	
	private Set<Student4> students=new HashSet<Student4>();

	public Course4() {
	}

	public Course4(Integer id, String name) {
		this.id = id;
		this.name = name;
	}

	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<Student4> getStudents() {
		return students;
	}

	public void setStudents(Set<Student4> students) {
		this.students = students;
	}

	public String toString() {
		return "Course [id=" + id + ", name=" + name + "]";
	}

}
2.新建Course4.hbm.xml

<?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 package="com.iflytek.model4">
	<class name="Course4">
		<!-- 配置哪个属性 关联数据表主键 -->
		<id name="id" column="id" type="integer">
			<generator class="native"></generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="64" />
		</property>
		<!-- 通过table属性告诉hibernate中间表,cascade指明级联操作的类型,inverse属性说明Course4实体类是被控方,不负责维护关系表 ,不能触发对象和数据库的同步更新的。-->
		<set name="students" table="student_course" cascade="save-update,delete">
			<key column="id"></key>
			<many-to-many class="Student4" column="sid"></many-to-many>
		</set>
	</class>
</hibernate-mapping>
3.新建Student4.java

public class Student4 {

	private Integer sid; // 学生id
	private String name; // 学生姓名
	private String gender; // 学生性别
	private Date birthday; // 学生出生日期
	
	private Set<Course4> courses = new HashSet<Course4>();

	public Student4() {

	}

	public Integer getSid() {
		return sid;
	}



	public void setSid(Integer sid) {
		this.sid = sid;
	}



	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}



	public String getGender() {
		return gender;
	}



	public void setGender(String gender) {
		this.gender = gender;
	}



	public Date getBirthday() {
		return birthday;
	}



	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}



	public Set<Course4> getCourses() {
		return courses;
	}



	public void setCourses(Set<Course4> courses) {
		this.courses = courses;
	}



	public String toString() {
		return "Student4 [sid=" + sid + ", name=" + name + ", gender=" + gender
				+ ", birthday=" + birthday + "]";
	}
	
}

4.新建Student4.hbm.xml

<?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 package="com.iflytek.model4">
	<class name="Student4">
		<!-- 配置哪个属性 关联数据表主键 -->
		<id name="sid" column="sid" type="integer">
			<generator class="native"></generator>
		</id>
		<property name="name" type="string">
			<column name="name" length="64" />
		</property>

		<property name="gender" type="string">
			<column name="gender"></column>
		</property>
		<property name="birthday" type="timestamp">
			<column name="birthday"></column>
		</property>

		<!-- 通过table属性告诉hibernate中间表,cascade指明级联操作的类型,inverse属性说明Student4实体类是主控方,负责维护关系表 -->
		<set name="courses" table="student_course" cascade="save-update,delete">
			<key column="sid"></key>
			<many-to-many class="Course4" column="id"></many-to-many>
		</set>

	</class>
</hibernate-mapping>
5.测试类 TestManyToMany.java

public class TestManyToMany {
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init(){
		//创建配置对象,加载配置文件 hibernate.cfg.xml
		Configuration configuration = new Configuration().configure();
		//创建会话连接工厂
		sessionFactory = configuration.buildSessionFactory();
		//打开会话
		session = sessionFactory.openSession();
		//开启事务
		transaction = session.beginTransaction();
	}
	

	@After
	public void destory(){
		//提交事务
		transaction.commit();
		//关闭会话
		session.close();
		//关闭工厂会话连接
		sessionFactory.close();
		
	}

	/**
	 * 新增学生 和课程信息
	 */
	@Test
	public void saveStudent() {
		Student4 stu = new Student4();
		stu.setName("张三丰");
		stu.setGender("男");
		stu.setBirthday(new Date());
		
		Student4 stu2 = new Student4();
		stu2.setName("张亮");
		stu2.setGender("男");
		stu2.setBirthday(new Date());
		
		Course4 c1=new Course4();  
		c1.setName("English");  
		Course4 c2=new Course4();  
		c2.setName("science");  
		
		stu.getCourses().add(c1);  
		stu.getCourses().add(c2);  
		
		stu2.getCourses().add(c1);  
		stu2.getCourses().add(c2);
		
		
		session.save(stu); 
		session.save(stu2); 
	}

	/**
	 * 更新学生信息
	 */
	@Test
	public void testUpdateStu() {
		// 获取一个学生对象
		Student4 stu = (Student4) session.get(Student4.class, 1);
		stu.setName("张峰");
		session.save(stu);
	}

	/**
	 * 删除学生信息
	 */
	@Test
	public void testDeleteStu() {
		Student4 stu = (Student4) session.get(Student4.class, 1);
		session.delete(stu);
	}

	/**
	 * 查询学生信息
	 */
	@Test
	public void testGetStu() {
		Student4 stu = (Student4) session.get(Student4.class, 1);
		System.out.println(stu);
	}
}







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值