jpa技术注释技术的一个实例

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
public class ProductType implements Serializable {
 /**
  *
  */
 private static final long serialVersionUID = 1L;

 
 private Integer typeid;
 
 private String name;
 //用于google描述
 private String note;
 private Boolean visible = true;
 /**
  * 子类别
  */
 private Set<ProductType> childtypes= new HashSet<ProductType>();
 /**
  * 父类别
  * @return
  */
 private ProductType parent;
 
 public ProductType(){
  
 }
 public ProductType(int typeid){
  this.typeid = typeid;
 }
 public ProductType(String name,String note){
  this.name = name;
  this.note = note;
 }
 
 @OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="parent")//mappedBy是说父类是被管的
 public Set<ProductType> getChildtypes() {
  return childtypes;
 }

 public void setChildtypes(Set<ProductType> childtypes) {
  this.childtypes = childtypes;
 }
 @ManyToOne(cascade={CascadeType.REFRESH},optional=true)//optional=false说明父类是可选的,不是每个子类都必须有父类的
 @JoinColumn(name="parentid")//在子类生成的表中添加一个外键字段parentid,指向父类的表(也可以是同一张表)
 public ProductType getParent() {
  return parent;
 }

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

 @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 + ((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,object,jpa,null,Integer,equals)