1. 程式人生 > >hibernate表關係配置——一對多

hibernate表關係配置——一對多

如:訂單表與訂單項表

訂單表對應的實體類
(與訂單項表為一對多的關係)

    private Integer orderId;
	private String orderNo;
	
	/*
	 * 在描述關係的時候,一定是集合介面進行接受
	 * 對應的訂單詳情一對多關係
	 * 不一定要用set list也可以
	 */
	private Set<OrderItems> orderItems = new HashSet<OrderItems>();
	
	//解決懶載入的問題
	private Integer initOrderItems = 0;//0懶載入  1立即載入
	
	public Integer getInitOrderItems() {
		return initOrderItems;
	}
	public void setInitOrderItems(Integer initOrderItems) {
		this.initOrderItems = initOrderItems;
	}
	public Set<OrderItems> getOrderItems() {
		return orderItems;
	}
	public void setOrderItems(Set<OrderItems> 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;
	}

訂單項表對應的實體類
(與訂單表為多對一的關係)

    private Integer orderItemId;//訂單項id
	private Integer productId;//商品id
	private Integer quantity ;//數量
	private Integer oid;//訂單id
	
//	對應的訂單
	private 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 Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}
	

訂單表的配置檔案

<?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>
<!-- 
	table:實體類對應的表名 
	name :實體類的全類名
-->
	<class table="t_hibernate_order" name="com.liu.four.entity.Order">
	<!-- 
	    name : 實體類的變數名
	    column : 資料庫的列段名
	    type : 變數的資料型別			
	 -->
		<id name="orderId" column="order_id" type="java.lang.Integer">
			<generator class="increment"></generator>
		</id>
		<property name="orderNo" column="order_no" type="java.lang.String"/>
	
	<!-- 
		name : 在order中的orderItems的orderitem的變數名
		cascade : 配置的是級聯關係
		inverse : 反方,將級聯關係交給對方控制嗎?
		lazy : 是否懶載入	lazy="false" 懶載入浪費資源
	 -->
	<set name="orderItems" cascade="save-update" inverse="true">
		<!-- 配置外來鍵欄位 -->
		<key column="oid"></key>
		<!-- 外來鍵表對應的實體類 -->
		<one-to-many class="com.liu.four.entity.OrderItems"/>
	</set>
	
	</class>
</hibernate-mapping>

訂單項表對應的配置檔案

<?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.liu.four.entity.OrderItems">
		<id name="orderItemId" column="order_item_id" type="java.lang.Integer">
			<generator class="increment"></generator>
		</id>
		<property name="productId" column="product_id" type="java.lang.Integer"/>
		<property name="quantity" column="quantity" type="java.lang.Integer"/>
		<property name="oid" column="oid" type="java.lang.Integer" insert="false" update="false"/>
		
		<!--  -->
		<many-to-one name="order" class="com.liu.four.entity.Order" column="oid"></many-to-one>
	</class>
</hibernate-mapping>

配置好之後要記得在主配置檔案中配置

<!-- 關係一對多 -->
		<mapping resource="com/liu/four/entity/Order.hbm.xml"/>
		<mapping resource="com/liu/four/entity/OrderItems.hbm.xml"/>