Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

One To One Mapping

By: akhilknambiar on Jul 27th, 2012  |  syntax: Java  |  size: 0.87 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. @Entity
  2. @Table(name = "Post")
  3. public class Post {
  4.  
  5.         private int id;
  6.         private Content content;
  7.  
  8.         @Id
  9.         @GeneratedValue(strategy = GenerationType.IDENTITY)
  10.         public int getId() {
  11.                 return id;
  12.         }
  13.  
  14.         @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  15.         @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "content_id")
  16.         public Content getContent() {
  17.                 return content;
  18.         }
  19.  
  20.         public void setContent(Content content) {
  21.                 this.content = content;
  22.         }
  23. }
  24.  
  25.  
  26.  
  27. @Entity
  28. @Table(name = "Content")
  29. public class Content {
  30.  
  31.         private int content_id;
  32.  
  33.         @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "post1"))
  34.         @Id
  35.         @GeneratedValue(generator = "generator")
  36.         public int getContent_id() {
  37.                 return content_id;
  38.         }
  39.  
  40.         public void setContent_id(int content_id) {
  41.                 this.content_id = content_id;
  42.         }
  43.  
  44. }