巴巴运动网 17 无限级产品分类的双向一对多JPA映射


首先:结合 巴巴运动网 16,

结合 巴巴运动网 16节 增添

在 ProductType.java类中,增添如下属性:


    /** 子类别 **/
    private Set<ProductType> childtypes = new HashSet<ProductType>();

    /** 所属 父类 **/
    private ProductType parent;

    // optional=true 可以选择吗 ? 这里指的是:可以没有父类。
    @ManyToOne(cascade = CascadeType.REFRESH, optional = true)
    @JoinColumn(name = "parentid")  // 此为 外键。
    public ProductType getParent() {
        return parent;
    }

    public void setParent(ProductType parent) {
        this.parent = parent;
    }

    // 1,cascade为级联。2,JPA规定 : 一的这一端为 关系被维护端。mappedBy="" 其值为 维护端的 标识。也就是
    // 用parent负责关系的维护。
    @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.REMOVE }, mappedBy = "parent")
    public Set<ProductType> getChildtypes() {
        return childtypes;
    }

    public void setChildtypes(Set<ProductType> childtypes) {
        this.childtypes = childtypes;
    }


package com.itcast.bean;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class ProductType implements Serializable{
	/** 类别 id **/
	private Integer typeid;

	/** 类别名称 **/
	private String name;

	/** 搜索google 页面存放的 内容 **/
	private String note;

	/** 备注 **/
	private boolean visible = true;

	/** 子类别 **/
	private Set<ProductType> childtypes = new HashSet<ProductType>();

	/** 所属 父类 **/
	private ProductType parent;

	// optional=true 可以选择吗 ? 这里指的是:可以没有父类。
	@ManyToOne(cascade = CascadeType.REFRESH, optional = true)
	@JoinColumn(name = "parentid")  // 此为 外键。
	public ProductType getParent() {
		return parent;
	}

	public void setParent(ProductType parent) {
		this.parent = parent;
	}

	// 1,cascade为级联。2,JPA规定 : 一的这一端为 关系被维护端。mappedBy="" 其值为 维护端的 标识。也就是
	// 用parent负责关系的维护。
	@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.REMOVE }, mappedBy = "parent")
	public Set<ProductType> getChildtypes() {
		return childtypes;
	}

	public void setChildtypes(Set<ProductType> childtypes) {
		this.childtypes = childtypes;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getTypeid() {
		return typeid;
	}

	public void setTypeid(Integer typeid) {
		this.typeid = typeid;
	}

	@Column(length = 36, nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length = 200)
	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	@Column(nullable = false)
	public boolean getVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((childtypes == null) ? 0 : childtypes.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((note == null) ? 0 : note.hashCode());
		result = prime * result + ((parent == null) ? 0 : parent.hashCode());
		result = prime * result + ((typeid == null) ? 0 : typeid.hashCode());
		result = prime * result + (visible ? 1231 : 1237);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProductType other = (ProductType) obj;
		if (childtypes == null) {
			if (other.childtypes != null)
				return false;
		} else if (!childtypes.equals(other.childtypes))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (note == null) {
			if (other.note != null)
				return false;
		} else if (!note.equals(other.note))
			return false;
		if (parent == null) {
			if (other.parent != null)
				return false;
		} else if (!parent.equals(other.parent))
			return false;
		if (typeid == null) {
			if (other.typeid != null)
				return false;
		} else if (!typeid.equals(other.typeid))
			return false;
		if (visible != other.visible)
			return false;
		return true;
	}

	
}


1,说明:因为EJB3.0是分布式的,要通过网络传递,所以这里要序列化。。


2,如果:

 // 1,cascade为级联。2,JPA规定 : 一的这一端为 关系被维护端。mappedBy="" 其值为 维护端的 标识。也就是
    // 用parent负责关系的维护。
    @OneToMany(cascade = { CascadeType.REFRESH, CascadeType.REMOVE }, mappedBy = "parent")


这句话,写到 set()方法上去了,回报如下异常:这里都是写在 get()方法上。


Caused by: org.hibernate.MappingException: Could not determine type

for: java.util.Set, at table: ProductType, for columns:

[org.hibernate.mapping.Column(childtypes)]
    at org.hibernate.mapping.SimpleValue.getType

(SimpleValue.java:292)
    at org.hibernate.mapping.SimpleValue.isValid

(SimpleValue.java:276)
    at org.hibernate.mapping.Property.isValid(Property.java:207)
    at org.hibernate.mapping.PersistentClass.validate

(PersistentClass.java:458)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:215)
    at org.hibernate.cfg.Configuration.validate

(Configuration.java:1135)
    at org.hibernate.cfg.Configuration.buildSessionFactory

(Configuration.java:1320)
    at

org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory

(AnnotationConfiguration.java:867)
    at

org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory

(Ejb3Configuration.java:669)
    ... 61 more



3,@JoinColumn(name = "parentid")  // 此为 外键。



测试类:

package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.itcast.bean.ProductType;
import com.itcast.service.product.ProductService;

public class ProductTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test
	public void runtest() {
		
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		ProductService productService = (ProductService) cxt.getBean("productServiceBean");
		ProductType type = new ProductType();
		type.setName("瑜伽用品");
		type.setNote("很好,不错");
		productService.save(type);
		
	}

}

结果成功:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值