hibernate中List一对多映射关系详解
场景:一个Team对一个多个Student,其中Team中的studes属性为List类型
直接上代码,测试通过的:
Team.java
package com.fgh.hibernate;
import java.util.ArrayList;
import java.util.List;
/**
* 一的一方
* @author fgh
*
*/
@SuppressWarnings("unchecked")
public class Team {
private String id;
private String name;
private List students = new ArrayList();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
}
Studeng.java
package com.fgh.hibernate;
/**
* 多的一方
* @author fgh
*
*/
public class Student {
private String id;
private String cardId;
private String name;
private int age;
private Team team;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
}
以下三个配置文件均放在src根目录下:
hibernate主配置文件:
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<!-- 引入两个相关配置文件 -->
<mapping resource="Team.hbm.xml" />
<mapping resource="Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
一方配置文件: Team.hbm.xml
<?xml version="1.0"?>
<!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.fgh.hibernate.Team" table="team_list">
<!-- 采用uuid生成主键 这里要指定type为string类型 -->
<id name="id" column="id" type="string">
<generator class="uuid"></generator>
</id>
<!-- 像类似这样的name属性 都是指类里面的成员变量 column指数据库中对应的字段名-->
<property name="name" column="name" type="string"></property>
<!-- 设置级联为 cascade="all"-->
<list name="students" table="student_list" cascade="all">
<!-- 维护对方的外键 -->
<key column="team_id"></key>
<!-- index标签表示多的一方 对应一的一方的索引位置
column属性表示数据库中存放数据的字段
index为关键字 避免跟关键字冲突 使用`index` 或 [index]
-->
<index column="[index]" type="string"></index>
<!-- 建立一对多的关联 -->
<one-to-many class="com.fgh.hibernate.Student" />
</list>
</class>
</hibernate-mapping>
多方配置文件 : Studeng.hbm.xml
<?xml version="1.0"?>
<!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.fgh.hibernate.Student" table="student_list">
<id name="id" column="id" type="string">
<generator class="uuid"></generator>
</id>
<property name="cardId" column="card_id" type="string"></property>
<property name="name" column="name" type="string"></property>
<property name="age" column="age" type="int"></property>
<!-- 因为list中的value 不是原始类型 所以要指定其类型 -->
<!-- 建立多对一关联映射关系 column="team_id"表示team这张表通过该字段
和student表关联 -->
<many-to-one name="team" column="team_id"
class="com.fgh.hibernate.Team">
</many-to-one>
</class>
</hibernate-mapping>
根据以上三个配置文件,可以生成对应的数据库表,代码如下:
CreateTable.java
package com.fgh.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 根据.hbm.xml配置文件创建相应的数据库表
* @author fgh
*
*/
public class CreateTable {
public static void main(String[] args) {
SchemaExport export = new SchemaExport(new Configuration().configure());
// 第一个参数表示是否在控制台打印出DDL语句(print the DDL to the console)
// 第二个参数表示是否将脚本输出到数据库(export the script to the database)
export.create(true, true);
}
}
运行CreateTable.java 生成对应表,后台打印sql如下:
alter table student_list drop foreign key FKFEDE142211CB0B6A
drop table if exists student_list
drop table if exists team_list
create table student_list (id varchar(255) not null, card_id varchar(255), name varchar(255), age integer, team_id varchar(255), `index` integer, primary key (id))
create table team_list (id varchar(255) not null, name varchar(255), primary key (id))
alter table student_list add index FKFEDE142211CB0B6A (team_id), add constraint FKFEDE142211CB0B6A foreign key (team_id) references team_list (id)
ok,表已经创建成功,下面测试保存操作:
InsertTest.java
package com.fgh.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* 保存操作测试类
* @author fgh
*
*/
public class InsertTest {
private static SessionFactory sessionFactory;
//创建工厂实例
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
//创建两个team
Team team = new Team();
team.setName("team1");
Team team2 = new Team();
team2.setName("team2");
//创建6个student对象
Student student = new Student();
student.setAge(20);
student.setName("zhangsan");
student.setTeam(team);
Student student2 = new Student();
student2.setAge(24);
student2.setName("lisi");
student2.setTeam(team);
Student student3 = new Student();
student3.setAge(24);
student3.setName("student3");
student3.setTeam(team2);
Student student4 = new Student();
student4.setAge(24);
student4.setName("student4");
student4.setTeam(team2);
Student student5 = new Student();
student5.setAge(24);
student5.setName("student5");
student5.setTeam(team2);
//前两个student属于team
team.getStudents().add(student);
team.getStudents().add(student2);
//后三个student属于team2
team2.getStudents().add(student3);
team2.getStudents().add(student4);
team2.getStudents().add(student5);
//开启事务
tx = session.beginTransaction();
//保存team和team2
session.save(team);
session.save(team2);
System.out.println("save success!");
//提交事务
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (null != tx) {
tx.rollback();
}
} finally {
session.close();
}
}
}
控制打印sql如下:
save success!
Hibernate: insert into team_list (name, id) values (?, ?)
Hibernate: insert into student_list (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student_list (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into team_list (name, id) values (?, ?)
Hibernate: insert into student_list (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student_list (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into student_list (card_id, name, age, team_id, id) values (?, ?, ?, ?, ?)
Hibernate: update student_list set team_id=?, `index`=? where id=?
Hibernate: update student_list set team_id=?, `index`=? where id=?
Hibernate: update student_list set team_id=?, `index`=? where id=?
Hibernate: update student_list set team_id=?, `index`=? where id=?
Hibernate: update student_list set team_id=?, `index`=? where id=?
02-04插入的是team
05-08插入的是team2
09-13是对student_list表中外键team_id,和排序字段index的维护
student_list表中记录如下:
id name age team_id
8a8adb7b34a45b700134a45b72130002 zhangsan 20 8a8adb7b34a45b700134a45b71fc00010
8a8adb7b34a45b700134a45b72130003 lisi 24 8a8adb7b34a45b700134a45b71fc00011
8a8adb7b34a45b700134a45b72130005 student3 24 8a8adb7b34a45b700134a45b721300040
8a8adb7b34a45b700134a45b72130006 student4 24 8a8adb7b34a45b700134a45b721300041
8a8adb7b34a45b700134a45b72140007 student5 24 8a8adb7b34a45b700134a45b721300042
team_list表中记录:
8a8adb7b34a467320134a46733cc0001 team1
8a8adb7b34a467320134a46733e40004 team2