Hibernate的单向一对多
作者:互联网
Hibernate关系映射-单向一对多
单向一对多会多执行update语句,效率不如多对一。
一、学生表和年级表
Student表
id | name | age | grade_id |
---|---|---|---|
1 | 张三 | 20 | 1 |
2 | 李四 | 22 | 1 |
3 | 王五 | 22 | 2 |
Grade表
id | name |
---|---|
1 | 基础 |
2 | 中级 |
二、Student类和Grade类
-
先创建一个Student类
public class Student implements Serializable { private Integer id; private String name; private Integer age; // private Grade grade; @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + // ", grade=" + grade + '}'; } 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } // public Grade getGrade() { // return grade; // } // // public void setGrade(Grade grade) { // this.grade = grade; // } }
-
创建一个Grade类,用的是set集合。
public class Grade implements Serializable { private Integer id; private String name; private Set<Student> students = new HashSet<>(); @Override public String toString() { return "Grade{" + "id=" + id + ", name='" + name + '\'' + ", students=" + students + '}'; } 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.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.uestc"> <class name="Student"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <property name="age"/> <!-- 多对一 name对应的字段名称 class对应的类型 not-null不可以为空 column外键列名 foreign-key外键名称 --> <!-- <many-to-one name="grade" class="Grade" not-null="true"--> <!-- column="grade_id" foreign-key="fk_grade"/>--> </class> </hibernate-mapping>
-
创建Grade.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.uestc"> <class name="Grade"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <!-- 单向一对多关系 set:Grade类中的集合属性 name:集合属性名称 key:外键 column:外键列名 foreign-key:生成外键约束的名字(可以不写,默认随机) not-null="true" 不可以为空(可以不写,默认false) one-to-many:Grade类中属性students所表示类型 --> <set name="students"> <key foreign-key="fk_grade" column="grade_id" not-null="true"></key> <one-to-many class="Student"/> </set> </class> </hibernate-mapping>
-
在src和test下面的resources下面创建hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 初始化JDBC连接 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property> <!-- 数据库生成方式 --> <property name="hbm2ddl.auto">update</property> <!-- validate检测,create-drop删了重新创建,create重新建表,update表如果存在插数据,如果不存在建表插数据 --> <!-- 打印sql --> <property name="show_sql">true</property> <!-- 格式化sql --> <property name="format_sql">true</property> <!-- 关联对象配置文件 --> <mapping resource="com/uestc/Grade.hbm.xml"/> <mapping resource="com/uestc/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
-
测试类,测试单向一对多和是否获取到Grade下面的基础和名字是否对应。
/** * 单向一对多 */ @Test public void testSingleOneToMany() { Session session = null; Transaction tx = null; try { session = HibernateUtil.getSession(); tx = session.beginTransaction(); //创建实例对象 Student student = new Student(); student.setName("张三"); student.setAge(18); Student student2 = new Student(); student2.setName("李四"); student2.setAge(18); Student student3 = new Student(); student3.setName("王五"); student3.setAge(18); Grade grade1 = new Grade(); grade1.setName("基础"); grade1.getStudents().add(student); grade1.getStudents().add(student2); Grade grade2 = new Grade(); grade2.setName("中级"); grade2.getStudents().add(student3); //存储的顺序是根据外键约束而定的,如果外键不可以为空,必须先存储外键的一端 //单向一对多会多执行update语句,效率不如多对一 session.save(grade1); session.save(grade2); session.save(student); session.save(student2); session.save(student3); tx.commit(); }catch (Exception e){ e.printStackTrace(); tx.rollback(); }finally { HibernateUtil.closeSession(); } } /** * 获取 * 查询不用开启事务,会降低性能 */ @Test public void testSingleGetOneToMany() { Session session = null; Transaction tx = null; try { session = HibernateUtil.getSession(); tx = session.beginTransaction(); Grade grade = session.get(Grade.class,1); System.out.println("grade:" +grade.getName()); for(Student stu : grade.getStudents()){ System.out.println("stuName:" +stu.getName()); } tx.commit(); }catch (Exception e){ e.printStackTrace(); tx.rollback(); }finally { HibernateUtil.closeSession(); }
-
数据库最后得到的结果。
标签:Hibernate,name,Grade,单向,public,session,Student,一对,id 来源: https://www.cnblogs.com/mhy123/p/16225553.html