一、创建实体类
package com.wr.hibernate.entity;
import java.util.HashSet;
import java.util.Set;
public class Custom {
private int customId;
private String customName;
private Set<Order> orders = new HashSet<Order>();
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
public int getCustomId() {
return customId;
}
public void setCustomId(int customId) {
this.customId = customId;
}
public String getCustomName() {
return customName;
}
public void setCustomName(String customName) {
this.customName = customName;
}
public Custom(int customId, String customName) {
super();
this.customId = customId;
this.customName = customName;
}
public Custom() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Custom [customId=" + customId + ", customName=" + customName + "]";
}
}
package com.wr.hibernate.entity;
public class Order {
private int orderId;
private String orderName;
private Custom custom;
public Custom getCustom() {
return custom;
}
public void setCustom(Custom custom) {
this.custom = custom;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
@Override
public String toString() {
return "Order [orderId=" + orderId + ", orderName=" + orderName + ", custom=" + custom + "]";
}
}
二、POJO配置文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wr.hibernate.entity"> <class name="Custom" table="custom"> <id name="customId" type="java.lang.Integer"> <column name="custom_id" /> <!-- 指定主键的生成方式, native: 使用数据库本地方式 --> <generator class="native" /> </id> <property name="customName" type="java.lang.String"> <column name="custom_name" /> </property> <!-- 配置多一端集合, 配置inver让多一端维护关联关系 --> <set name="orders" inverse="true"> <key column="custom_id"></key> <one-to-many class="Order"/> </set> </class> </hibernate-mapping>
package com.wr.hibernate.entity; public class Order { private int orderId; private String orderName; private Custom custom; public Custom getCustom() { return custom; } public void setCustom(Custom custom) { this.custom = custom; } public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } @Override public String toString() { return "Order [orderId=" + orderId + ", orderName=" + orderName + ", custom=" + custom + "]"; } }