1. 程式人生 > >Hibernate:級聯新增與

Hibernate:級聯新增與

Hibernate:級聯新增

案例講解: 訂單和訂單項 1.1:訂單類:

public class Order {
	private Integer orderId;
 	private String orderNo;
 	//定義一對多的關係時一定需要採用介面方式,不許使用實現類
	//implements java.util.Set  代理實現的是set介面
        private Set<OrderItem> orderItems=new HashSet<OrderItem>();

	public Set<OrderItem> getOrderItems() {
	  return orderItems;
	 }
	 public void setOrderItems(Set<OrderItem> 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;
	 }
}

1.2:訂單項類:

public class OrderItem {
	private Integer orderItemId;
	 private Integer productId;
	 private Integer quaneity;
	 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 getQuaneity() {
	  return quaneity;
	 }
	 public void setQuaneity(Integer quaneity) {
	  this.quaneity = quaneity;
	 }
	 public Integer getOid() {
	  return oid;
	 }
	 public void setOid(Integer oid) {
	  this.oid = oid;
	 }
}

1.3:order.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>		
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-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>
      
      <!-- 
      name:實體類的類屬性
      cascade:用來配置維護實體類之間的關係所用
      inverse:(inverse="true")關係交由反方控制    即訂單項   因為現在配置的是訂單,所以反方是訂單項
      (inverse="true") 關係由自己控制   訂單
      lazy="false":立即載入
       -->
      <set name="orderItems" cascade="save-update" inverse="true">
       <!-- 訂單項外來鍵 -->
       <key column="oid"></key>
       <!-- 一個訂單對應多個訂單項 -->
       <one-to-many class="com.zking.four.entity.OrderItem"></one-to-many>
      </set>
     </class>
    </hibernate-mapping>

1.4:oederItem.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
     <class table="t_hibernate_order_item" name="com.zking.four.entity.OrderItem">
      <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="quaneity" type="java.lang.Integer" column="quantity "></property>
      <!--解決報錯方案:-->
      <!-- 第一種加insert=false,update=false -->
      <property name="oid" type="java.lang.Integer" column="oid" insert="false" update="false"></property>
      <!-- 第二種加insert=false,update=false -->
      <!-- <property name="oid" type="java.lang.Integer" column="oid"></property> -->
      <!-- 多對一 
      order:orderItem裡的屬性,也是實體類
      oid:實體類的屬性所對應的後臺的欄位
         -->
      <!-- 會報錯 :解決方法:第一種加,第二種加-->
      <!-- 第一種加insert=false,update=false -->
     <many-to-one name="order" class="com.zking.four.entity.Order" column="oid"></many-to-one>
     <!-- 第二種加insert=false,update=false -->
     <!-- <many-to-one name="order" class="com.zking.four.entity.Order" column="oid" insert="false" update="false"></many-to-one> -->
     </class>	     	     	     
</hibernate-mapping>

1.5:hibernate.cfg.xml

<?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>

1.6:dao方法:

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;
	 }
}

1.6:junit測試:

public class OrderDaoTest {
 private OrderDao orderDao=new OrderDao();
 private OrderItem orderItem=new OrderItem();
 private Order order=new Order();
 @Test
 public void testAddOrderItem() {
  //第一種加insert="false" update="false"
  orderItem.setProductId(45);
  orderItem.setQuaneity(99);
  order.setOrderId(1);
  orderItem.setOrder(order);
  this.orderDao.addOrderItem(orderItem);
  
  //第二種加insert="false" update="false"
//  orderItem.setOid(1);
//  orderItem.setProductId(54);
//  orderItem.setQuaneity(91);
//  this.orderDao.addOrderItem(orderItem);
 }
}

當 property 沒有設定insert和update屬性時會報錯:

<property name="oid" type="java.lang.Integer" column="oid" ></property>

報錯: Repeated(重複) column in mapping for entity: com.zking.four.entity.OrderItem column: oid (should be mapped with insert=“false” update=“false”)

  • 原因:同一個欄位(外來鍵oid在orderItem.hbm.xml中)被映射了兩次
  • 解決方案:
  • 1、刪除從表對應的實體類中的外來鍵屬性 (把order裡的oid刪除) 2、在配置的xml中外來鍵屬性上新增 insert=false,update=false的設定。 3、在配置的xml中的manyToOne標籤中新增insert=false,update=false的設定。
  • orderItem.hbm.xml裡的欄位重複了 oid

在這裡插入圖片描述