hibernate 注解 一对多 例子

package com.xcj.admin.entity.shop;

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.ManyToOne;

import javax.persistence.OneToOne;

import javax.persistence.SequenceGenerator;

import javax.persistence.Table;

 

import com.sun.istack.internal.NotNull;

import com.xcj.admin.entity.area.FoursArea;

@Entity(name="FoursShop")

@Table(name="fours_shop")

@SequenceGenerator(name="sequenceGenerator",sequenceName="secquence_fours_shop")

public class FoursShop   implements java.io.Serializable{

 

    private static final long serialVersionUID = 1L;

     

    public Integer id;

 

    private String  name;//机构名称

 

    private String  address;//地址

 

    private String  desccription;//描述信息

 

    private java.util.Date  createDate;//创建时间

 

    private java.util.Date  modifyDate;//修改时间

 

    private FoursArea foursArea;

 

    

@NotNull

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

@JoinColumn(name="area_id")

    public FoursArea getFoursArea() {

return foursArea;

}

public void setFoursArea(FoursArea foursArea) {

this.foursArea = foursArea;

}

    @Id

@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequenceGenerator")

public Integer getId() {

return id;

}

 

public void setId(Integer id) {

this.id = id;

}

    

@Column(name="name",nullable = false, length=200)

    public String  getName(){

       return name;

    }

 

    public  void  setName(String name){

       this.name=name;

    }

 

 

    @Column(name="address",nullable = false, length=255)

    public String  getAddress(){

       return address;

    }

 

    public  void  setAddress(String address){

       this.address=address;

    }

 

 

    @Column(name="desccription",nullable = true, length=255)

    public String  getDesccription(){

       return desccription;

    }

 

    public  void  setDesccription(String desccription){

       this.desccription=desccription;

    }

 

 

    @Column(name="create_date",nullable = false, length=19)

    public java.util.Date  getCreateDate(){

       return createDate;

    }

 

    public  void  setCreateDate(java.util.Date createDate){

       this.createDate=createDate;

    }

 

 

    @Column(name="modify_date",nullable = false, length=19)

    public java.util.Date  getModifyDate(){

       return modifyDate;

    }

 

    public  void  setModifyDate(java.util.Date modifyDate){

       this.modifyDate=modifyDate;

    }

 

 

 

 

}

/------------------------

package com.xcj.admin.entity.area;

import java.util.Date;

import java.util.HashSet;

import java.util.Set;

 

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.OneToMany;

import javax.persistence.SequenceGenerator;

import javax.persistence.Table;

 

import com.xcj.admin.entity.shop.FoursShop;

@Entity(name="FoursArea")

@Table(name="fours_area")

@SequenceGenerator(name="sequenceGenerator",sequenceName="secquence_fours_area")

public class FoursArea   implements java.io.Serializable{

   private static final long serialVersionUID = 1L;

  

   public Integer id;

   

    private String  code;//编码

 

    private String  name;//名称

    

    private String leader;//区域负责人

    

    private String mobile;//负责人联系电话

 

    private String  description;//描述

 

    private Date  createDate;//创建日期

 

    private Date  modifyDate;//修改日期

 

    private Integer  partnerId;//合作伙伴外键

    private Set<FoursShop> foursShops = new HashSet<FoursShop>();

    @OneToMany(mappedBy = "foursArea", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)

public Set<FoursShop> setFoursShops() {

return foursShops;

}

 

public void setFoursShops(Set<FoursShop> foursShops) {

this.foursShops = foursShops;

}

 

    @Id

@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequenceGenerator")

public Integer getId() {

return id;

}

 

public void setId(Integer id) {

this.id = id;

}

 

@Column(name="leader",nullable = false, length=50)

public String getLeader() {

return leader;

}

 

 

public void setLeader(String leader) {

this.leader = leader;

}

 

@Column(name="mobile",nullable = false, length=15) 

public String getMobile() {

return mobile;

}

 

 

public void setMobile(String mobile) {

this.mobile = mobile;

}

 

@Column(name="code",nullable = false, length=100)

    public String  getCode(){

       return code;

    }

 

    public  void  setCode(String code){

       this.code=code;

    }

 

 

    @Column(name="name",nullable = false, length=255)

    public String  getName(){

       return name;

    }

 

    public  void  setName(String name){

       this.name=name;

    }

 

 

    @Column(name="description",nullable = true, length=255)

    public String  getDescription(){

       return description;

    }

 

    public  void  setDescription(String description){

       this.description=description;

    }

 

 

    @Column(name="create_date",nullable = true, length=19)

    public java.util.Date  getCreateDate(){

       return createDate;

    }

 

    public  void  setCreateDate(java.util.Date Date){

       this.createDate=Date;

     }

 

 

    @Column(name="modify_date",nullable = true, length=19)

    public java.util.Date  getModifyDate(){

       return modifyDate;

    }

 

    public  void  setModifyDate(java.util.Date modifyDate){

       this.modifyDate=modifyDate;

    }

 

 

    @Column(name="partner_id",nullable = false, length=19)

    public Integer  getPartnerId(){

       return partnerId;

    }

 

    public  void  setPartnerId(Integer partnerId){

       this.partnerId=partnerId;

    }

 

 

 

}

 

你可能感兴趣的:(Hibernate)