views:

186

answers:

2

Hello All...

I have one table that has composite key 'rid' and 'sid'.

For that i have made following beans to map with hibernate annotations :

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 has following code :

@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;
    }
}

My WBListDao has following method :

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

Following is my controller code :

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

When I am executing above code, the wbList fetching the null value..

If anybody have any solutions, plz help..

Thanks in advance...

+1  A: 

I'm not sure if assigning two @Id columns would work. Better look at @EmbeddedId

Bozho
Can you plz provide any example link that shows the same ?
Nirmal
http://www.java2s.com/Code/Java/EJB3/MarkEmbeddedId.htm
Bozho
A: 

Got the solutions, with some other way of implementation of Composite Key in Hibernate...

Following are the code that illustrate the solution :

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;
    }

Controller Code :

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