Hibernate自关联的配置与实现

这段时间在学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();
	}
到此,一个简单的菜单导航分类便实现了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值