关联指的是类之间的引用关系。如果类A与类B关联,那么被引用的类B将被定义为类A的属性。例如:
class B{
private String name;
private List bs = new arraylist();
}
public class A{
private B b = new B;
public A(){}
}
关联可以分为一对一、一对多/多对一、多对多关联
使用t_hibernate_order表和t_hibernate_order_item表
导进实体映射文件并建立实体类并且建立关联关系
Order.hbm.xml
OrderItem.hbm.xml(后需修改)
Order
package com.tzx.three.entity;
import java.util.ArrayList;
import java.util.List;
public class Order {
private Integer orderId;
private String orderNo;
private List orderItems = new ArrayList<>();
private Integer initChildren = 1;//1代表开启懒加载 0代表关闭懒加载
public Integer getInitChildren() {
return initChildren;
}
public void setInitChildren(Integer initChildren) {
this.initChildren = initChildren;
}
public List getOrderItems() {
return orderItems;
}
public void setOrderItems(List orderItems) {
this.orderItems = orderItems;
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public Order() {
super();
}
public Order(Integer orderId, String orderNo) {
super();
this.orderId = orderId;
this.orderNo = orderNo;
}
}
OrderItem
package com.tzx.three.entity;
public class OrderItem {
private Integer orderItemId;
private Integer productId;
private Integer quantity;
private Integer oid;
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Integer getOrderItemId() {
return orderItemId;
}
public void setOrderItemId(Integer orderItemId) {
this.orderItemId = orderItemId;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {
super();
this.orderItemId = orderItemId;
this.productId = productId;
this.quantity = quantity;
this.oid = oid;
}
public OrderItem() {
super();
}
@Override
public String toString() {
return "OrderItem [orderItemId=" + orderItemId + ", productId=" + productId + ", quantity=" + quantity
+ ", oid=" + oid + ", order=" + order + "]";
}
}
hibernate.cfg.xml配置
准备测试:
DemoDao
package com.tzx.three.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.tzx.three.entity.Order;
import com.tzx.three.entity.OrderItem;
import com.tzx.two.uitl.SessionFactoryUtils;
public class DemoDao {
/**
* 为了测试关系型映射文件配置准确
* 讲解insert=false,update=false的用途
* @param order
* @return
*/
public Integer addOrder(Order order) {
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
Integer oid = (Integer)session.save(order);
transaction.commit();
session.close();
return oid;
}
public Integer addOrderItem(OrderItem orderItem) {
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
Integer otid = (Integer)session.save(orderItem);
transaction.commit();
session.close();
return otid;
}
/**
* 为了讲解懒加载的问题(hibernate3.0后所有查询方式默认采用的是懒加载方式)
* 1、查单个时存在问题,代理对象已经关闭
* 2、查多个存在问题,有性能的问题
* @param order
* @return
*/
public Order getOrder(Order order) {
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
Order o = session.get(Order.class, order.getOrderId());
// if(o != null && new Integer(0).equals(order.getInitChildren())) {
// Hibernate.initialize(o.getOrderItems());
System.out.println(o.getOrderItems());
// }
transaction.commit();
session.close();
return o;
}
public List getOrderList() {
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
List list = session.createQuery("from Order").list();
transaction.commit();
session.close();
return list;
}
/**
* z主表的数据不能随便删除,得先删除从表中对应信息,才能删除主表的信息。
* @param order
*/
public void delOrder(Order order) {
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
Order order2 = session.get(Order.class, order.getOrderId());
for (OrderItem oi : order2.getOrderItems()) {
session.delete(oi);
}
session.delete(order2);
// session.delete(order);
transaction.commit();
session.close();
}
}
使用junit进行单个测试
增加一个订单与订单项
public void testAddOrder() {
Order order = new Order();
order.setOrderNo("tanzhixian");
for (int i = 0; i < 3; i++) {
OrderItem oi = new OrderItem();
oi.setProductId(11+i);
oi.setQuantity(22+i);
oi.setOrder(order);
order.getOrderItems().add(oi);
}
demoDao.addOrder(order);
// demoDao.addOrderItem(orderItem)
}
直接运行会出一个常见的错误
Repeated column in mapping for entity: com.tzx.three.entity.OrderItem column: oid (should be mapped with insert=“false” update=“false”)
原因:映射文件映射了两次column=“oid”
解决方法
insert="false" update="false"
OrderItem.hbm.xml
t_hibernate_order
t_hibernate_order_item(三条数据oid为10)
给订单id为5的数据增加订单项
public void testAddOrderItem() {
Order order = new Order();
order.setOrderId(5);
for (int i = 0; i < 3; i++) {
OrderItem oi = new OrderItem();
oi.setProductId(11+i);
oi.setQuantity(22+i);
oi.setOrder(order);
order.getOrderItems().add(oi);
demoDao.addOrderItem(oi);
}
}
懒加载
/*
* 关于懒加载
* hiberbate3之后出现的
*/
@Test
public void testGetOrder() {
Order order = new Order();
order.setOrderId(10);
Order o = this.demoDao.getOrder(order);
System.out.println(o.getOrderNo());
System.out.println(o.getOrderItems());
}
错误:failed to lazily initialize a collection of role: com.tzx.three.entity.Order.orderItems, could not initialize proxy - no Session
原因:
orderNo数据来源于t_hibernate_order
orderItems数据来源于t_hibernate_order_item
hiberbate默认的策略:是不查询关联关系对应关联表数据的
解决方法:
不推荐使用这种
在Order.hbm.xml中bag标签加入lay=“false”
弊端:如果说将懒加载直接关闭lazy=“false”,那么在查询多条订单数据的情况下是非常影响性能的
推荐一种方法:
例:
查单个需要关闭懒加载 目的是需要加载出关联表的数据
查多个不能关闭懒加载 加载关联表的数据的查询次数过多
实体类Order定义一个开关,1代表开启懒加载 0代表关闭懒加载
将上面DemoDao中注释代码释放
@Test
public void testGetOrderList() {
List orderList = this.demoDao.getOrderList();
for (Order order : orderList) {
System.out.println(order.getOrderNo());
// System.out.println(order.getOrderItems());
}
}
删除
@Test
public void testDelOrder() {
Order order = new Order();
order.setOrderId(10);
this.demoDao.delOrder(order);
}