hiberbate一對多的級聯新增和級聯查詢與級聯刪除
阿新 • • 發佈:2018-12-17
1. 什麼是關聯(association) 1.1 關聯指的是類之間的引用關係。如果類A與類B關聯,那麼被引用的類B將被定義為類A的屬性。例如: public class A{ private B b = new B; public A(){} } 1.2 關聯的分類:關聯可以分為一對一、一對多/多對一、多對多關聯 關聯是有方向的
- 案例:如何建立客戶和訂單一對多雙向關聯 2.1 先建立客戶和訂單的關聯關係,定義實體及對映檔案,單獨執行儲存操作 2.2 建立客戶到訂單的一對多關聯關係
2.3 建立訂單到客戶的多對一關聯關係
<!--1.註釋 2.只讀--> <property name="cid" type="java.lang.Integer" column="cid" insert="false" update="false"> </property>
2.4 注意:在Hibernate當中定義實體物件的集合屬性時,只能使用介面而不能使用類 級聯新增 外來鍵處理的三種方式 1、刪除從表對應的實體類中的外來鍵屬性 2、在配置的xml中外來鍵屬性上新增 insert=false,update=false的設定。 3、在配置的xml中的manyToOne標籤中新增insert=false,update=false的設定。 級聯新增 casecade=save-update 介紹
級聯查詢 配置檔案介紹以及後臺sql的形成過程 級聯查詢時的問題 Lazy=true介紹 查單個時存在問題 Lazy=false介紹 查所有時存在問題 解決方案:通過欄位控制,強制載入。Hibernate.initialize()
普通刪除 Order Add 講外來鍵的處理 Get/list order.getOrderItems.size講懶載入的處理,sql形成過程 Del 將關係的處理中的刪除
**一對多主表xml建立程式碼**
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-mapping> <class table="t_hibernate_order" name="com.zking.four.entity.Order"> <id name="orderId" type="java.lang.Integer" column="order_id"> <generator class="increment"></generator> </id> <property name="orderNo" type="java.lang.String" column="order_no"></property> <!-- cascade:用來維護實體類之間的關係所用 inverse:關係交由反方控制 --> <set name="orderItems" cascade="save-update" inverse="true"> <!-- column:填外來鍵 --> <key column="oid"></key> <one-to-many class="com.zking.four.entity.OrderItem"></one-to-many> </set> </class> </hibernate-mapping>
**一對多從表xml建立程式碼**
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-mapping>
<!--
table:實體類所對應表
name:實體類的全類名
-->
<class table="t_hibernate_order_item" name="com.zking.four.entity.OrderItem">
<!--
name:實體類的屬性
type:實體類的屬性型別
column:指的是資料庫表的主鍵列
-->
<id name="orderitemId" type="java.lang.Integer" column="order_item_id">
<generator class="increment"></generator>
</id>
<property name="productId" type="java.lang.Integer" column="product_id"></property>
<property name="quantity" type="java.lang.Integer" column="quantity"></property>
<!--
oid:oid為主表與從表的關聯屬性
insert="false" update="false":代表該屬性不能新增(insert)也不能修改(update)只能由主表控制
-->
<property name="oid" type="java.lang.Integer" column="oid" insert="false" update="false"></property>
<many-to-one name="order" class="com.zking.four.entity.Order" column="oid">
</many-to-one>
</class>
</hibernate-mapping>
dao方法
import java.io.Serializable;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zking.four.entity.Order;
import com.zking.four.entity.OrderItem;
import com.zking.two.util.SessionFactoryUtils;
public class OrderDao {
//增加訂單項
public Integer addOrderItem(OrderItem orderitem) {
Session session = SessionFactoryUtils.getSession();
Transaction transaction= session.beginTransaction();
Integer otid = (Integer)session.save(orderitem);
transaction.commit();
session.close();
return otid;
}
//增加訂單
public Integer addOrder(Order order) {
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
Integer otid = (Integer) session.save(order);
transaction.commit();
session.close();
return otid;
}
//查詢
public Order getOrder(Order order) {
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
Order o = session.get(Order.class,order.getOrderId());
if(o!=null&&new Integer(1).equals(order.getInitorderItems())) {
Hibernate.initialize(o.getOrderItems());
}
transaction.commit();
session.close();
return o;
}
public List<Order> getOrderList(){
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
List<Order> list = session.createQuery("from Order").list();
transaction.commit();
session.close();
return list;
}
public void delOrder(Order order) {
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
Order o = session.get(Order.class, order.getOrderId());
for (OrderItem ot : o.getOrderItems()) {
session.delete(ot);
}
session.delete(o);
transaction.commit();
session.close();
}
}
**測試程式碼**
```package com.zking.four.dao;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import com.zking.four.entity.Order;
import com.zking.four.entity.OrderItem;
public class OrderDaoTest {
private OrderDao orderdao=new OrderDao();
@Test
public void testAddOrderItem() {
OrderItem orderItem=new OrderItem();
// orderItem.setOid(10);
orderItem.setProductId(66);
orderItem.setQuantity(56);
Order order=new Order();
order.setOrderId(1);
orderItem.setOrder(order);;
this.orderdao.addOrderItem(orderItem);
}
@Test
public void testAddOrder() {
Order order=new Order();
order.setOrderNo("jh");
OrderItem orderItem;
for (int i = 1; i <=6; i++) {
orderItem=new OrderItem();
orderItem.setProductId(665);
orderItem.setQuantity(566);
order.getOrderItems().add(orderItem);
orderItem.setOrder(order);
}
this.orderdao.addOrder(order);
}
//查當個
@Test
public void testgetOrder() {
Order order=new Order();
order.setOrderId(2);
order.setInitorderItems(1);
Order o = this.orderdao.getOrder(order);
System.out.println(o.getOrderNo());
for (OrderItem ot : o.getOrderItems()) {
System.out.println(ot.getProductId());
}
}
//查所有
@Test
public void testgetOrderlist() {
List<Order> orderList = this.orderdao.getOrderList();
for (Order o : orderList) {
System.out.println(o.getOrderNo());
for (OrderItem ot : o.getOrderItems()) {
System.out.println(ot.getProductId());
}
}
}
//查所有
@Test
public void testDelOrder() {
Order order=new Order();
order.setOrderId(1);
this.orderdao.delOrder(order);
}
}