一、创建是实体类
package com.wr.hibernate.entity;
public class Custom {
private int customId;
private String customName;
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> </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"> <hibernate-mapping package="com.wr.hibernate.entity"> <class name="Order" table="orders"> <id name="orderId" type="java.lang.Integer"> <column name="order_id" /> <!-- 指定主键的生成方式, native: 使用数据库本地方式 --> <generator class="native" /> </id> <property name="orderName" type="java.lang.String"> <column name="order_name" /> </property> <!-- 指定一那端的外键 --> <many-to-one name="custom" class="Custom"> <column name="customId"></column> </many-to-one> </class> </hibernate-mapping>