Category类(一方)
import java.util.HashSet;
import java.util.Set;
/**
* 商品分类表
* @author Administrator
*
*/
public class Category {
private int id;
private String name;
private String description;
private Set product = new HashSet();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set getProduct() {
return product;
}
public void setProduct(Set product) {
this.product = product;
}
}
Product类(多方)
/**
* 具体的商品表(单向的一对多所以此类不关联Category)
* @author Administrator
*
*/
public class Product {
private int id;
private String name;
private Double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
Category.hbm.xml文件
<?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.lbx.model"> <class name="Category" table="category" lazy="true"> <id name="id" type="int" column="ID"> <generator class="increment"/> </id> <property name="name" type="java.lang.String" column="NAME"/> <property name="description" type="java.lang.String" /> <set name="product" > <key column="category_id" /> <one-to-many class="com.lbx.model.Product"/> </set> </class> </hibernate-mapping>
Product.hbm.xml文件
<?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.lbx.model"> <class name="Product" table="product" lazy="true"> <id name="id" type="int" column="ID"> <generator class="increment"/> </id> <property name="name" type="java.lang.String" column="NAME"/> <property name="price" type="java.lang.Double" column="PRICE"/> </class> </hibernate-mapping>