实体类:
package com.zking.entity2;
import java.util.HashSet;
import java.util.Set;
public class Order {
// create table t_hibernate_order
// (
// order_id int primary key auto_increment,
// order_no varchar(50) not null
// );
private Integer orderId;
private String orderNo;
//一对多就是多
//注意:变量属性一定用接口进行接受
private Set<OrderItem> orderItems = new HashSet<OrderItem>();
private Integer initOrderItems = 0;//0代表懒加载,1代表强制加载
public Integer getInitOrderItems() {
return initOrderItems;
}
public void setInitOrderItems(Integer initOrderItems) {
this.initOrderItems = initOrderItems;
}
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;
}
}
package com.zking.entity2;
public class OrderItem {
// create table t_hibernate_order_item
// (
// order_item_id int primary key auto_increment,
// product_id int not null,
// quantity int not null,
// oid int not null,
// foreign key(oid) references t_hibernate_order(order_id)
// );
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;
}
}
配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-11-25 19:59:59 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity2.Order" table="t_hibernate_order">
<id name="orderId" type="java.lang.Integer">
<column name="order_id" />
<generator class="increment" />
</id>
<property name="orderNo" type="java.lang.String">
<column name="order_no" />
</property>
<set name="orderItems" table="ORDERITEM" inverse="false" lazy="true" cascade="all">
<key>
<column name="oid" />
</key>
<one-to-many class="com.zking.entity2.OrderItem" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-11-25 19:59:59 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity2.OrderItem" table="t_hibernate_order_item">
<id name="orderItemId" type="java.lang.Integer">
<column name="order_item_id" />
<generator class="increment" />
</id>
<property name="productId" type="java.lang.Integer">
<column name="product_id" />
</property>
<property name="quantity" type="java.lang.Integer">
<column name="quantity" />
</property>
<property name="oid" type="java.lang.Integer">
<column name="oid" />
</property>
<many-to-one name="order" class="com.zking.entity2.Order" cascade="all" insert="false" update="false">
<column name="oid" />
</many-to-one>
</class>
</hibernate-mapping>
测试:
@Test
public void oneToMony() {
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Order o = session.get(Order.class, 1);
Hibernate.initialize(o.getOrderItems());
System.out.println(o.getOrderNo());
System.out.println(o.getOrderItems().size());
session.getTransaction().commit();
}