1.表结构
CREATE TABLE `product_category` (
`product_category_id` int(11) NOT NULL,
`product_category_name` varchar(255) NOT NULL,
`product_total_number` int(11) NOT NULL,
`parent_id` int(11) NOT NULL COMMENT '父级ID',
`is_parent` bit(1) NOT NULL COMMENT '是否还有子级',
PRIMARY KEY (`product_category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table product_category add index fk_category_parent_id (parent_id),
alter table product_category add constraint foreign_category_parent_id foreign key (parent_id) references product_category(product_category_id);
2、实体
package com.eywa.domain; import java.util.Set; public class ProductCategory implements java.io.Serializable,Comparable<ProductCategory>{ private static final long serialVersionUID = 1381736107052831957L; private Integer productCategoryId; //商品种类ID private String productCategoryName; //商品种类名称 private Integer productTotalNumber; //该类商品数量 private Integer parentId; //父级ID private Boolean isParent; //是否还有子级 private ProductCategory parentProductCategory;//父 private Set<ProductCategory> childProductCategory;//子分类集合 public ProductCategory(){ } public int compareTo(ProductCategory o) { return productCategoryId>o.getProductCategoryId()?1:(productCategoryId==o.getProductCategoryId()?0:-1); } public Integer getProductCategoryId() { return productCategoryId; } public void setProductCategoryId(Integer productCategoryId) { this.productCategoryId = productCategoryId; } public String getProductCategoryName() { return productCategoryName; } public void setProductCategoryName(String productCategoryName) { this.productCategoryName = productCategoryName; } public Integer getProductTotalNumber() { return productTotalNumber; } public void setProductTotalNumber(Integer productTotalNumber) { this.productTotalNumber = productTotalNumber; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } public Boolean getIsParent() { return isParent; } public void setIsParent(Boolean isParent) { this.isParent = isParent; } public Set<ProductCategory> getChildProductCategory() { return childProductCategory; } public void setChildProductCategory(Set<ProductCategory> childProductCategory) { this.childProductCategory = childProductCategory; } public ProductCategory getParentProductCategory() { return parentProductCategory; } public void setParentProductCategory(ProductCategory parentProductCategory) { this.parentProductCategory = parentProductCategory; } }
3、实体对应的配置文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.eywa.domain.ProductCategory" table="product_category" > <id name="productCategoryId" type="java.lang.Integer"> <column name="product_category_id" /> <generator class="assigned" /> </id> <many-to-one name="parentProductCategory" column="parent_id" cascade="all" class="com.eywa.domain.ProductCategory"></many-to-one> <set name="childProductCategory" cascade="all" inverse="true"> <key column="parent_id"></key> <one-to-many class="com.eywa.domain.ProductCategory"/> </set> <property name="productCategoryName" type="java.lang.String"> <column name="product_category_name" not-null="true"> <comment></comment> </column> </property> <property name="productTotalNumber" type="java.lang.Integer"> <column name="product_total_number" not-null="true"> <comment></comment> </column> </property> <!-- <property name="parentId" type="java.lang.Integer"> <column name="parent_id" not-null="true"> <comment>父级ID</comment> </column> </property> --> <property name="isParent" type="java.lang.Boolean"> <column name="is_parent" not-null="true"> <comment>是否还有子级</comment> </column> </property> </class> </hibernate-mapping>