Pages

Sunday, 5 May 2013

One to many with composite key

First our parent class looks like this:
1:  import javax.persistence.*;  
2:    
3:  @Entity  
4:  @Table(name="Parent")  
5:  public class Parent {  
6:     
7:   @EmbeddedId  
8:   private ParentPk parentPk = new ParentPk();  
9:     
10:   @Column(name="NormalField")  
11:   private String normalField;  
12:     
13:   public Parent(){}  
14:     
15:   public Parent(String field) {  
16:    this.normalField = field;  
17:   }  
18:     
19:   public ParentPk getParentPk() {  
20:    return parentPk;  
21:   }  
22:    
23:   public void setParentPk(ParentPk parentPk) {  
24:    this.parentPk = parentPk;  
25:   }  
26:    
27:   public String getNormalField(){  
28:        return normalField;  
29:   }  
30:    
31:   public void setNormalField(String field){  
32:        normalField = field;  
33:   }   
34:  }  
And key class for the parent:
1:  import java.io.Serializable;  
2:    
3:  import javax.persistence.Column;  
4:    
5:  public class ParentPk implements Serializable{  
6:    
7:   private static final long serialVersionUID = 2194116512563143369L;  
8:    
9:   @Column(name="FirstKey")  
10:   private int firstKey;  
11:     
12:   @Column(name="SecondKey")  
13:   private String secondKey;  
14:    
15:   public ParentPk(){}  
16:     
17:   public ParentPk(int firstKey, String secondKey, String area) {  
18:    this.firstKey = firstKey;  
19:    this.secondKey = secondKey;  
20:   }  
21:    
22:   public int getFirstKey() {  
23:    return firstKey;  
24:   }  
25:    
26:   public void setFirstKey(int firstKey) {  
27:    this.firstKey = firstKey;  
28:   }  
29:    
30:   public String getSecondKey() {  
31:    return secondKey;  
32:   }  
33:    
34:   public void setSecondKey(String secondKey) {  
35:    this.secondKey = secondKey;  
36:   }  
37:     
38:  }  
And the child class:
1:  @Entity  
2:  @Table(name="Child")  
3:  public class Child{  
4:    
5:   @Id  
6:   @Column(name="Id")  
7:   private int id;       
8:    
9:   @Column(name="ChildField")  
10:   private String childField;  
11:     
12:   @Column(name="FirstKey",insertable=false,updatable=false)  
13:   private int firstKey;  
14:     
15:   @Column(name="SecondKey",insertable=false,updatable=false)  
16:   private String secondKey;  
17:    
18:   public int getId() {  
19:    return id;  
20:   }  
21:    
22:   public void setId(int id) {  
23:    this.id = id;  
24:   }  
25:     
26:   public String getChildField() {  
27:    return childField;  
28:   }  
29:    
30:   public void setChildField(String childField) {  
31:    this.childField = childField;  
32:   }  
33:     
34:   public int getFirstKey() {  
35:    return firstKey;  
36:   }  
37:    
38:   public void setFirstKey(int firstKey) {  
39:    this.firstKey = firstKey;  
40:   }  
41:    
42:   public String getSecondKey() {  
43:    return secondKey;  
44:   }  
45:    
46:   public void setSecondKey(String secondKey) {  
47:    this.secondKey = secondKey;  
48:   }  
49:    
50:   public Parent getParent() {  
51:    return parent;  
52:   }  
53:    
54:   public void setParent(Parent parent) {  
55:    this.parent = parent;  
56:   }  
57:    
58:   @ManyToOne  
59:   @JoinColumns({  
60:    @JoinColumn(name="FirstKey",referencedColumnName="FirstKey"),  
61:    @JoinColumn(name="SecondKey",referencedColumnName="SecondKey")  
62:   })  
63:   Parent parent;  
64:  }  

No comments:

Post a Comment