HibernateTemplate 复合键获取值 null

Posted

技术标签:

【中文标题】HibernateTemplate 复合键获取值 null【英文标题】:HibernateTemplate Composite Key fetching value null 【发布时间】:2009-12-11 08:37:48 【问题描述】:

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

为此,我制作了以下 bean 以使用休眠注释进行映射:

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

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

提前谢谢...

【问题讨论】:

【参考方案1】:

我不确定分配两个 @Id 列是否可行。 最好看看@EmbeddedId

【讨论】:

【参考方案2】:

得到了解决方案,在 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);

【讨论】:

以上是关于HibernateTemplate 复合键获取值 null的主要内容,如果未能解决你的问题,请参考以下文章

尝试插入重复的键行时,HibernateTemplate Save Not Throwing Exception

使用复合键保存实体获取 ConversionNotSupportedException

如何通过复合键获取项目?

SQL 更新:无法更改第一条记录的复合键值之一

带有复合键的 Rails RESTful 路由

Spark reduceByKey() 返回一个复合值