2009-12-11 84 views
0

我有一張複合關鍵字'rid'和'sid'的表。HibernateTemplate複合關鍵字獲取值null

對於我做了以下豆與Hibernate映射註釋:

WBList.java 
============ 
@Entity 
@IdClass(WBListPK.class) 
public class WBList { 
    private int rid; 
    private int sid; 
    private String wb; 

    @Id 
    @JoinColumn(name="rid") 
    public int getRid() { 
     return rid; 
    } 
    public void setRid(int rid) { 
     this.rid = rid; 
    } 
    @Id 
    @JoinColumn(name="sid") 
    public int getSid() { 
     return sid; 
    } 
    public void setSid(int sid) { 
     this.sid = sid; 
    } 
    @Column(name="wb") 
    public String getWb() { 
     return wb; 
    } 
    public void setWb(String wb) { 
     this.wb = wb; 
    } 
} 

WBListPK.java有下面的代碼:

@Embeddable 
public class WBListPK implements Serializable { 
    private int rid; 
    private int sid; 
    private String wb; 

    public int getRid() { 
     return rid; 
    } 
    public void setRid(int rid) { 
     this.rid = rid; 
    } 
    public int getSid() { 
     return sid; 
    } 
    public void setSid(int sid) { 
     this.sid = sid; 
    } 
    public String getWb() { 
     return wb; 
    } 
    public void setWb(String wb) { 
     this.wb = wb; 
    } 
} 

我WBListDao有以下方法:

//Some other code ... 
public WBList getWBListById(WBListPK wbListPK) { 
     return (WBList) this.hibernateTemplate.get(WBList.class,wbListPK); 
    } 

以下是我的控制器代碼:

WBList wbList = new WBList(); 
WBListPK wbListPK = new WBListPK(); 
wbListPK.setRid(1); 
wbListPK.setSid(7); 
wbList = this.wbListSecurityProcessor.getWBListById(wbListPK); 
System.out.println("Wblist = "+wbList); 

當我上面的代碼執行時,wbList取空值..

如果任何人有任何解決方案,plz幫助..提前

謝謝...

回答

1

得到了解決方案,以實現在Hibernate中複合鍵的一些其他的方式...

下面是說明解決方案的代碼:

WBList.java

@Entity 
public class WBList { 

    private WBListPK id; 
    private String wb; 
    private Integer rid; 
    private Integer sid; 

    @Id 
    public WBListPK getId() { 
     return id; 
    } 
    public void setId(WBListPK id) { 
     this.id = id; 
    } 
    @Column(name="wb") 
    public String getWb() { 
     return wb; 
    } 
    public void setWb(String wb) { 
     this.wb = wb; 
    } 
} 

WBListPK.java

@Embeddable 
public class WBListPK implements Serializable { 

    private int rid; 
    private int sid; 

    public WBListPK() { 
    } 

    public WBListPK(Integer rid, Integer sid) { 
     this.rid = rid; 
     this.sid = sid; 
    } 

    public int getRid() { 
     return rid; 
    } 

    public void setRid(int rid) { 
     this.rid = rid; 
    } 

    public int getSid() { 
     return sid; 
    } 

    public void setSid(int sid) { 
     this.sid = sid; 
    } 

控制器代碼:

WBList wbList = new WBList(); 
wbList.setWb("d"); 
WBListPK wbListPK = new WBListPK(30,40); 
wbList.setId(wbListPK); 
this.wbListSecurityProcessor.save(wbList); 
1

我不知道分配兩個@Id列是否可行。 最好看看@EmbeddedId

+0

你能提供任何示例鏈接,顯示相同的? – Nirmal 2009-12-11 11:30:23

+0

http://www.java2s.com/Code/Java/EJB3/MarkEmbeddedId.htm – Bozho 2009-12-11 11:32:51