一对多:主外键关系
多对多:提供中间表 提供两个字段(外键)分别对应两个主表
一对一:主外键关系
一对多案例
1,编写实体类
顾客类:
package org.ccit.com.domain;
import java.util.HashSet;
import java.util.Set;
/**
* @program: Hibernate_01
* @description
* @author: LIANG
* @create: 2021-03-02 21:11
**/
public class Customer {
private Integer id; //客户id
private String name; //客户名称
//一个多属性描述 一个客户有n个订单
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;
}
}
订单类:
package org.ccit.com.domain;
/**
* @program: Hibernate_01
* @description
* @author: LIANG
* @create: 2021-03-02 21:13
**/
public class Order {
private Integer id;//订单id 工作中订单id一般都uuid类型
private Integer name;//订单名称
private Customer customer;//订单值属于一个客户
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getName() {
return name;
}
public void setName(Integer name) {
this.name = name;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
2,编写映射文件
Customer.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.ccit.com.domain.Customer" table="t_Customer">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name" length="20"></property>
<set name="orders">
<!-- 填写Order表中的外键-->
<key column="customer_id"></key>
<one-to-many class="org.ccit.com.domain.Order"></one-to-many>
</set>
</class>
</hibernate-mapping>
Order.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.ccit.com.domain.Order" table="t_order">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<many-to-one name="customer" class="org.ccit.com.domain.Customer" column="customer_id"></many-to-one>
</class>
</hibernate-mapping>
在Hibernate.cfg.xml文件中引入
<mapping resource="org/ccit/com/domain/Order.hbm.xml"></mapping>
<mapping resource="org/ccit/com/domain/Customer.hbm.xml"></mapping>
3,编写测试类
import org.ccit.com.domain.Customer;
import org.ccit.com.domain.Order;
import org.ccit.com.uitls.HibernateUtils;
import org.hibernate.Session;
import org.junit.Test;
/**
* @program: Hibernate_01
* @description
* @author: LIANG
* @create: 2021-03-03 17:24
**/
public class HibernateTest04 {
@Test
public void method01(){
Session session = HibernateUtils.openSession();
session.getTransaction().begin();
//创建顾客
Customer customer = new Customer();
customer.setName("张三");
//创建订单
Order order1 = new Order();
order1.setName("短袖");
Order order2 = new Order();
order2.setName("裤子");
//维护客户与订单的关系
customer.getOrders().add(order1);
customer.getOrders().add(order2);
//维护订单与客户的关系
order1.setCustomer(customer);
order2.setCustomer(customer);
session.save(customer); //执行三条insert语句 一个客户 两个订单
//Hibernate: insert into t_Customer (name) values (?)
//Hibernate: insert into t_order (name, customer_id) values (?, ?)
//Hibernate: insert into t_order (name, customer_id) values (?, ?)
session.save(order1); //维护外键关系 执行一条update语句
//Hibernate: update t_order set customer_id=? where id=?
session.save(order2); //维护外键关系 执行一条update语句
//Hibernate: update t_order set customer_id=? where id=?
session.getTransaction().commit();
session.close();
}
}
外键的维护方式
inverse是hibernate双向关系中的基本概念 inverse的真正作用就是指定由哪一方来维护关联关系 当一方中制定了”inverse=flase“(默认) 那么哪一方有责任负责之间的关联关系
<one-to-many>中,建议inverse=”true”,由“many”方来进行关联关系的维护 这样执行的sql语句更少
<many-to-many>中,只设置其中一方inverse=”false”,或双方都不设置
<set name="orders" inverse=”true”>
<!-- 填写Order表中的外键-->
<key column="customer_id"></key>
<one-to-many class="org.ccit.com.domain.Order"></one-to-many>
</set>