这段时间在学Hibernate,知道了Hibernate的一对一、一对多、多对多关系表的操作,今天在做毕业设计时,忽然觉得之前做的菜单关联用两张表设计不太合理,知道Hibernate可以实现单表自身关联之后琢磨一整个下午,终于实现了。
实体类代码,如下:
package com.liveshop.category.entity;
import java.util.HashSet;
import java.util.Set;
public class Category implements java.io.Serializable{
private Integer id;//商品ID
private String cname;//商品名称
private Category parent;//父菜单
private Set<Category> child =new HashSet<Category>();//子菜单
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Category getParent() {
return parent;
}
public void setParent(Category parent) {
this.parent = parent;
}
public Set<Category> getChild() {
return child;
}
public void setChild(Set<Category> child) {
this.child = child;
}
@Override
public String toString() {
return "Category [id=" + id + ", cname=" + cname + "]";
}
public Category(Integer id, String cname, Category parent, Set<Category> child) {
super();
this.id = id;
this.cname = cname;
this.parent = parent;
this.child = child;
}
public Category() {
super();
// TODO Auto-generated constructor stub
}
}
实体类.hbm.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.liveshop.category.entity.Category" table="categorys">
<id name="id">
<generator class="identity"></generator>
</id>
<property name="cname"></property>
<set name="child" cascade="save-update" inverse="true" lazy="false">
<key column="category_id"></key>
<one-to-many class="com.liveshop.category.entity.Category"/>
</set>
<many-to-one name="parent" column="category_id" class="com.liveshop.category.entity.Category" ></many-to-one>
</class>
</hibernate-mapping>
Service测试类:
package com.liveshop.category.service;
import java.util.List;
import java.util.Set;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.junit.Test;
import com.liveshop.category.entity.Category;
import com.liveshop.goods.entity.Goods;
import com.liveshop.user.utils.HibernateUtils;
public class CategoryService {
@Test
//添加一个父商品类
public void addCategory(){
Session session=HibernateUtils.openSession();
Transaction transaction=session.beginTransaction();
Category parent=new Category();
parent.setCname("婴儿食品");
session.save(parent);
transaction.commit();
session.close();
}
@Test
//往父商品类添加子商品类
public void addGoods(){
Session session=HibernateUtils.openSession();
Transaction transaction=session.beginTransaction();
Category parent=(Category) session.get(Category.class,21);
Category child1=new Category();
child1.setCname("蒙牛酸酸乳");
Category child2=new Category();
child2.setCname("伊利纯牛奶");
Category child3=new Category();
child3.setCname("美味蛋挞");
Category child4=new Category();
child4.setCname("豆奶");
//二级分类和一级分类关联
parent.getChild().add(child1);
parent.getChild().add(child2);
parent.getChild().add(child3);
parent.getChild().add(child4);
child1.setParent(parent);
child2.setParent(parent);
child3.setParent(parent);
child4.setParent(parent);
session.save(parent);
transaction.commit();
session.close();
}
到此,一个简单的菜单导航分类便实现了。