Hibernate03_多对多

x33g5p2x  于2021-12-25 转载在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(389)

Hibernate03_多对多

多对多关系

一门课程可以被多个学生选择,一个学生可以选择多门课程。
学生和课程是多对多关系。
在数据库中,学生和课程都是主表,中间表为从表。主表与中间表是一对多关系。

  • 数据表
    t_account表

t_course表

account_course中间表

  • 在实体类中:
  • Account
package com.blu.entity;

import java.util.Set;
import lombok.Data;

@Data
public class Account {
	private Integer id;
	private String name;
	private Set<Course> courses;
}
  • Course
package com.blu.entity;

import java.util.Set;
import lombok.Data;

@Data
public class Course {
	private Integer id;
	private String name;
	private Set<Account> accounts;
}
  • Account.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>
	<class name="com.blu.entity.Account" table="t_account">
		<id name="id" type="java.lang.Integer">
			<column name="id"></column>
			<generator class="identity"></generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name"></column>
		</property>
		<set name="courses" table="account_course">
			<key column="aid"></key>
			<many-to-many class="com.blu.entity.Course" column="cid"></many-to-many>
		</set>
	</class>

</hibernate-mapping>
  • 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>
	<class name="com.blu.entity.Course" table="t_course">
		<id name="id" type="java.lang.Integer">
			<column name="id"></column>
			<generator class="identity"></generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name"></column>
		</property>
		<set name="accounts" table="account_course">
			<key column="cid"></key>
			<many-to-many class="com.blu.entity.Account" column="aid"></many-to-many>
		</set>
	</class>

</hibernate-mapping>
  • 注册实体关系映射文件
<mapping resource="com/blu/entity/Account.hbm.xml"></mapping>
<mapping resource="com/blu/entity/Course.hbm.xml"></mapping>
  • 测试
package com.blu.test;

import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.blu.entity.Account;
import com.blu.entity.Course;

public class Test {
	public static void main(String[] args) {
		Configuration configuration = new Configuration().configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		
		Course course = new Course();
		course.setName("Java");
		
		Account account = new Account();
		account.setName("张三");
		Set<Course> courses = new HashSet<Course>();
		courses.add(course);
		account.setCourses(courses);
		
		session.save(course);
		session.save(account);
		session.beginTransaction().commit();
		session.close();
	}
	
}
  • 结果:
Hibernate: 
    insert 
    into
        t_course
        (name) 
    values
        (?)
Hibernate: 
    insert 
    into
        t_account
        (name) 
    values
        (?)
Hibernate: 
    insert 
    into
        account_course
        (aid, cid) 
    values
        (?, ?)
  • t_account

  • t_course

  • account_course

相关文章