gpt4 book ai didi

java - HibernateTemplate 复合键获取值 null

转载 作者:行者123 更新时间:2023-12-02 08:35:42 26 4
gpt4 key购买 nike

我有一张表,其中包含复合键“rid”和“sid”。

为此,我制作了以下 bean 来映射 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);
}

以下是我的 Controller 代码:

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 获取空值..

如果有人有任何解决方案,请帮忙..

提前致谢...

最佳答案

我不确定分配两个 @Id 列是否有效。更好地看看@EmbeddedId

关于java - HibernateTemplate 复合键获取值 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1886648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com