一对多关联关系映射
一对多映射关系是由“多"的一方指向”一“的一方。在表示”多“的一方数据表中增加一个外键,来指向”一“的一方的数据表,”一“的一方作为主表,而"多”的一方作为从表。
下面以客户和订单的关联关系来讲解。
首先是建立一个实体类Customer,其中包括客户属性id和name,Set集合orders是订单集合,表示一个客户有多个订单。
package cn.bruceluo.domain;
import java.util.HashSet;
import java.util.Set;
public class Customer {
private Integer id;
private String name;
//orders是集合,是对多的实体类的对象的集合,多个订单
private Set<Order> orders=new HashSet<Order>();
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<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
接着是order类的创建,其中包括订单的属性id、address和price
package cn.bruceluo.domain;
public class Order {
private Integer id;
private String address;
private Integer price;
//多的一方有对一的一方对象的引用,多个订单可以是一个人下
private Customer customer;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
完成类Customer和Order的创建后是创建他们的映射文件 Customer.hbm.xml和Order.hbm.xml,我这里使用了hibernate tools来创建,进行了适当的修改。
Customer.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">
<!-- Generated 2018-10-28 19:57:39 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="cn.bruceluo.domain.Customer" >
<id name="id" >
<generator class="native" />
</id>
<property name="name" length="20"/>
<!-- 配置了一个set元素 ,级联操作为all表示所有情况下均进行关联操作-->
<set name="orders" cascade="all" >
<key>
<!-- 外键取名cid -->
<column name="CID" />
</key>
<one-to-many class="cn.bruceluo.domain.Order" />
</set>
</class>
</hibernate-mapping>
Order.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">
<!-- Generated 2018-10-28 19:57:39 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="cn.bruceluo.domain.Order" table="orders">
<id name="id" >
<generator class="native" />
</id>
<property name="address" length="50" />
<property name="price" />
<!-- 配置一个many-to-one -->
<many-to-one name="customer" class="cn.bruceluo.domain.Customer" column="cid"/>
</class>
</hibernate-mapping>
接着是hibernate.cfg.xml配置文件的创建并添加 Customer.hbm.xml和Order.hbm.xml映射文件信息。另外我这里使用了<property name="hibernate.hbm2dd.auto" >update</property来进行自动建表。
<?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>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.hbm2dd.auto" >update</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
</property>
<property name="hibernate.show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8</property>
<mapping resource="cn/bruceluo/domain/Customer.hbm.xml"/>
<mapping resource="cn/bruceluo/domain/Order.hbm.xml"/>
</session-factory>
</hibernate-configuration>
配置好了,下面创建一个测试类One2ManyTest
package cn.bruceluo.test;
import static org.junit.Assert.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.bruceluo.domain.Customer;
import cn.bruceluo.domain.Order;
import cn.bruceluo.util.HibernateUtil;
public class One2ManyTest {
@Test
public void testInsert() {
//1.得到session
Session session =HibernateUtil.getSession();
//2.开启事务
session.beginTransaction();
//3.操作:插入操作
//3.1 创建一个客户,给客户属性赋值
Customer c=new Customer();
c.setName("bruceluo");
//3.2 创建两个订单
Order order1=new Order();
order1.setAddress("北京");
order1.setPrice(1000);
Order order2=new Order();
order2.setAddress("上海");
order2.setPrice(100);
//3.3 描述关系--客户拥有多个订单
c.getOrders().add(order2);
c.getOrders().add(order1);
//4.提交事务
session.save(c);
session.getTransaction().commit();
//5.关闭资源
session.close();
}
}
还有一点是我们要建立一个HibernateUtils的工具类来提供Session对象来防止产生过多的Session Factory实例
package cn.bruceluo.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final Configuration config;
private static final SessionFactory factory;
static{
config=new Configuration().configure();
System.out.println(config);
factory = config.buildSessionFactory();
}
public static Session getSession(){
return factory.openSession();
}
}
运行效果
多对多关联关系映射
多对多关联关系映射我们使用学生和课程的关联关系来讲解。Student类和Course类与前面Customer和Order类基本相同,属性有所改变而已。
Student.java
package cn.bruceluo.domain;
import java.util.HashSet;
import java.util.Set;
public class Student {
private Integer id;
private String sname;
private Set<Course> courses=new HashSet<Course>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
Course.java
package cn.bruceluo.domain;
import java.util.HashSet;
import java.util.Set;
public class Course {
private Integer id;
private String cname;
private Set<Student> students=new HashSet<Student>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
}
配置文件Student.hbm.xml和Course.hbm.xml与一对多配置方式不同的是<set>标签多了个table属性,这个属性表示中间表的名称我这里是s_c,<key>标签的column属性用于描述student表在中间表外键的名称,在<many-to-many>标签的column属性用于描述course课程表在中间表中的列名。
Student.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">
<!-- Generated 2018-11-1 22:26:40 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="cn.bruceluo.domain.Student" table="student">
<id name="id" >
<column name="id" />
<generator class="native" />
</id>
<property name="sname" >
</property>
<set name="courses" table="s_c" cascade="all">
<key>
<column name="sid" />
</key>
<many-to-many class="cn.bruceluo.domain.Course" column="cid"/>
</set>
</class>
</hibernate-mapping>
Course.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">
<!-- Generated 2018-11-1 22:26:40 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="cn.bruceluo.domain.Course" table="course">
<id name="id" >
<column name="id" />
<generator class="native" />
</id>
<property name="cname" >
</property>
<set name="students" table="s_c" >
<key>
<column name="cid" />
</key>
<many-to-many class="cn.bruceluo.domain.Student" column="sid"/>
</set>
</class>
</hibernate-mapping>
在上面的hibernate.cfg.xml中添加Student类和Course类的映射文件信息。
<mapping resource="cn/bruceluo/domain/Student.hbm.xml"/>
<mapping resource="cn/bruceluo/domain/Course.hbm.xml"/>
测试类Many2ManyTest的创建。
package cn.bruceluo.test;
import static org.junit.Assert.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.bruceluo.domain.Course;
import cn.bruceluo.domain.Customer;
import cn.bruceluo.domain.Order;
import cn.bruceluo.domain.Student;
import cn.bruceluo.util.HibernateUtil;
public class Many2ManyTest {
@Test
public void testInsert() {
//1.得到session
Session session =HibernateUtil.getSession();
//2.开启事务
Transaction transaction=session.beginTransaction();
//3.操作:插入操作
Student s1=new Student();
Student s2=new Student();
s1.setSname("bruceluo");
s2.setSname("bruceluo");
Course c1=new Course();
Course c2=new Course();
c1.setCname("JAVA");
c2.setCname("PHP");
//主控方关联关系
s1.getCourses().add(c1);
s1.getCourses().add(c2);
s2.getCourses().add(c1);
s2.getCourses().add(c2);
//4.提交事务
session.save(s1);
session.save(s2);
session.getTransaction().commit();
//5.关闭资源
session.close();
}
}
测试结果