`
xiangzhengyan
  • 浏览: 124663 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate Annotations 关联

阅读更多

如果People1定义了mappedBy,则在work表生成到People1的外键,work是这个关系的拥有者;
如果work定义了mappedBy,则在People1表生成到work的外键,People1是这个关系的拥有者。

在双向关联中,有且仅有一段是作为主体(owner)端在的:
主体端负责维护联接(即更新),对于不需要维护这种关系得从表则通过mappedBy进行声明。
OneToOne
There are three cases for one-to-one associations:
1.  either the associated entities share the same primary keys values,
2.  a foreign key is held by one of the entities (note that this FK column in the database should be constrained unique to simulate one-to-one multiplicity),
3.   a association table is used to store the link between the 2 entities (a unique constraint has to be defined on each fk to ensure the one to one multiplicity)
the first:share the same primary keys values
  1. @Entity  
  2. public class People1 implements java.io.Serializable {   
  3.   
  4.     private Integer id;   
  5.   
  6.     private String name;      
  7.   
  8.     private Work work;   
  9.   
  10.     @Id  
  11.     public Integer getId() {   
  12.         return id;   
  13.     }   
  14.   
  15.     public void setId(Integer id) {   
  16.         this.id = id;   
  17.     }   
  18.   
  19.     public String getName() {   
  20.         return name;   
  21.     }   
  22.   
  23.     public void setName(String name) {   
  24.         this.name = name;   
  25.     }   
  26.   
  27.     @OneToOne(cascade = CascadeType.ALL)   
  28.     @PrimaryKeyJoinColumn  
  29.     public Work getWork() {   
  30.         return work;   
  31.     }   
  32.   
  33.     public void setWork(Work work) {   
  34.         this.work = work;   
  35.     }   
  36.   
  37. @Entity  
  38. public class Work implements java.io.Serializable {   
  39.   
  40.     private int id;   
  41.   
  42.     private String corpname;   
  43.   
  44.     private People1 people1;   
  45.   
  46.     @OneToOne(mappedBy = "work")   
  47.     public People1 getPeople1() {   
  48.         return this.people1;   
  49.     }   
  50.   
  51.     public void setPeople1(People1 people1) {   
  52.         this.people1 = people1;   
  53.     }   
  54.   
  55.     public String getCorpname() {   
  56.         return corpname;   
  57.     }   
  58.   
  59.     public void setCorpname(String corpname) {   
  60.         this.corpname = corpname;   
  61.     }   
  62.   
  63.     @Id  
  64.     public int getId() {   
  65.         return id;   
  66.     }   
  67.   
  68.     public void setId(int id) {   
  69.         this.id = id;   
  70.     }  

分享到:
评论
1 楼 xiangzhengyan 2007-06-04  
双向 onetomany manytoone 有疑问

相关推荐

Global site tag (gtag.js) - Google Analytics