hibernate annotation 之 一对多、多对一双向外键关联

假设,一个农场产出多种植物,具体的某一植物产于某一农场。

  3 import java.io.Serializable;
  4 import java.util.Set;
  5 import javax.persistence.CascadeType;
  6 import javax.persistence.Column;
  7 import javax.persistence.Entity;
  8 import javax.persistence.FetchType;
  9 import javax.persistence.GeneratedValue;
 10 import javax.persistence.Id;
 11 import javax.persistence.OneToMany;
 12 /**
 13  * -----------------------------------------
 14  * @描述  农场
 17  * @日期  2012-10-21 <p>
 18  * -----------------------------------------
 19  */
 20 @Entity
 21 public class Farm implements Serializable{
 22
 23     private static final long serialVersionUID = 1L;
 24    
 25     private Integer id;
 26     private String  name;
 27     private Set<Plant> plantSet;
 28    
 29     @Id
 30     @GeneratedValue
 31     public Integer getId() {
 32         return id;
 33     }
 34     @Column(length = 18, nullable = false)
 35     public String getName() {
 36         return name;
 37     }
 38     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "farm")
 39     public Set<Plant> getPlantSet() {
 40         return plantSet;
 41     }
 42     public void setId(Integer id) {
 43         this.id = id;
 44     }
 45     public void setName(String name) {
 46         this.name = name;
 47     }
 48     public void setPlantSet(Set<Plant> plantSet) {
 49         this.plantSet = plantSet;
 50     }
 51 }
3 import java.io.Serializable;
  4 import javax.persistence.CascadeType;
  5 import javax.persistence.Column;
  6 import javax.persistence.Entity;
  7 import javax.persistence.FetchType;
  8 import javax.persistence.GeneratedValue;
  9 import javax.persistence.Id;
 10 import javax.persistence.ManyToOne;
 11 /**
 12  * -----------------------------------------
 13  * @描述  植物
 16  * @日期  2012-10-21 <p>
 17  * -----------------------------------------
 18  */
 19 @Entity
 20 public class Plant implements Serializable{
 21
 22     private static final long serialVersionUID = 1L;
 23    
 24     private Integer id;
 25     private String   name;
 26     private Farm    farm;
 27    
 28     @Id
 29     @GeneratedValue
 30     public Integer getId() {
 31         return id;
 32     }
 33     @Column(length = 18, nullable = false)
 34     public String getName() {
 35         return name;
 36     }
 37     @ManyToOne(cascade = CascadeType.PERSIST, optional = false, fetch = FetchType.LAZY)
 38     public Farm getFarm() {
 39         return farm;
 40     }
 41     public void setId(Integer id) {
 42         this.id = id;
 43     }
 44     public void setName(String name) {
 45         this.name = name;
 46     }
 47     public void setFarm(Farm farm) {
 48         this.farm = farm;
 49     }
 50
 51 }

 

转载于:https://www.cnblogs.com/panxueji/p/3163480.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值