无限级的双向一对多JPA映射(产品分类)

级联表

父类id 作为表的外键,外键指向到本身表的主键

类别实体bean定义

对象要在网络上传输必须先系列化

要进行对象之间的比较,所以要重载equals()hashCode()

ProductType.java

package com.itcast.bean.product;

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标注是一个实体bean

@Entity

public class ProductType implements Serializable{

    private static final long serialVersionUID = -6826720065899410840L;

    /** 类型id  *对象标识,字符串类型用UUID生成出来,可以用数值类型 */

    private Integer typeid;

    /** 类型名称 **/

    private String name;

    /** 备注,用作Google搜索页面描述 **/

    private String note;

    /** 是否可见 **/

    private Boolean visible=true;

    /** 子类别 **/

    private Set<ProductType> childtypes = new HashSet<ProductType>();

    /** 所属父类 **/

    private ProductType parent;

   

    @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;

    }

 

    @Id @GeneratedValue(strategy=GenerationType.AUTO)

    public Integer getTypeid() {

       return typeid;

    }

    public void setTypeid(Integer typeid) {

       this.typeid = typeid;

    }

 

    /**

     * 根据JPA规范,双向关系要定义关系维护端和被维护端,在一的一端用mappedBy定义被维护端,指向维护段(多的一方)的外键

     */

    @OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")

    public Set<ProductType> getChildtypes() {

       return childtypes;

    }

    public void setChildtypes(Set<ProductType> childtypes) {

       this.childtypes = childtypes;

    }

 

    /**

     * 可以用@JoinColumn()修改默认的关联外键字段名称

     * optional属性定义这个对象是否对该字段赋值,true表示可以为空,默认为true

     */

    @ManyToOne(cascade=CascadeType.REFRESH,optional=true)

    @JoinColumn(name="parentid")

    public ProductType getParent() {

       return parent;

    }

    public void setParent(ProductType parent) {

       this.parent = parent;

    }

@Override

    public int hashCode() {

       final int prime = 31;

       int result = 1;

       result = prime * result + ((typeid == null) ? 0 : typeid.hashCode());

       return result;

    }

 

    @Override

    public boolean equals(Object obj) {

       if (this == obj)

           return true;

       if (obj == null)

           return false;

       if (getClass() != obj.getClass())

           return false;

       final ProductType other = (ProductType) obj;

       if (typeid == null) {

           if (other.typeid != null)

              return false;

       } else if (!typeid.equals(other.typeid))

           return false;

       return true;

    }

 

}

你可能感兴趣的:(String,jpa,null,Integer,equals,产品)