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

来源:互联网 发布:淘宝懒人装修 编辑:程序博客网 时间:2024/05/01 15:44


首先:结合 巴巴运动网 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;@Entitypublic 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;}@Overridepublic 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;}@Overridepublic 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 {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Testpublic 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);}}

结果成功:



原创粉丝点击