小编典典

提供的休眠类型错误的ID

hibernate

我收到错误消息:

org.hibernate.TypeMismatchException:为类BEntity提供了错误类型的ID。预期:类BEntity,得到类AEntity

public class BEntity implements Serializable{
    @Id
    @Column(name = "NUM")
    private String num;

    @Id
    @Column(name = "INIT")
    private String init;

    @Column(name = "V_CNT")
    private Integer vcnt;

   //{{{some column omitted}}}//
}

public class AEntity implements Serializable{

    @Id
    @Column(name = "NUM")
    private String num;

    @Id
    @Column(name = "INIT")
    private String init;

    @OneToOne
    @PrimaryKeyJoinColumns({
        @PrimaryKeyJoinColumn(name="NUM", referencedColumnName="NUM"),
        @PrimaryKeyJoinColumn(name="INIT", referencedColumnName="INIT")
    })
    private BEntity bEntity;
}

HQL查询:

String queryString = "FROM AEntity AS A " +
                     "LEFT JOIN A.bEntityAS B " +
                     "WHERE A.INIT||A.NUM IN (:carList) AND A.INIT IN (:initList) AND A.NUM IN (:numberList) " + 
                     "AND B.TRUK_AXL_CNT > 0";

hibernate源代码

select aentity0_.NUMBER as NUMBER4_0_, aentity0_.INITIAL as INITIAL4_0_, bentity_p1_.NUMBER as NUMBER5_1_, bentity_p1_.INITIAL as INITIAL5_1_, aentity0_.V_CNT as VCNT3_4_0_, aentity0_.EIN as EIN4_0_, aentity0_.TYP as TYP5_4_0_, aentity0_.TRUK_CNT as TRUK6_4_0_, bentity_p1_.TRUK_AXL_CNT as TRUK3_5_1_ from USR.aentity aentity0_ left outer join USR.bentity_PRIMARY bentity_p1_ on aentity0_.NUMBER=bentity_p1_.NUMBER and aentity0_.INITIAL=bentity_p1_.INITIAL 
where (aentity0_.INITIAL||aentity0_.NUMBER in (?,?,?)) 
and (aentity0_.INITIAL in (?,?,?)) 
and (aentity0_.NUMBER in (?, ?, ?))
and bentity_p1_.TRUK_AXL_CNT>0

当我在SQL Explorer中运行代码时,只能在代码中运行它会导致问题…


阅读 196

收藏
2020-06-20

共1个答案

小编典典

看来这是hibernate3.2.6版中的一个缺陷,该缺陷仍未解决。碰到了这个JIRA

@IdHibernate支持具有多个,但似乎在一对一映射下会失败,解决此问题的建议方法是使用单个CompositeKey,这意味着您将创建PK类

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class PKClass implements Serializable {

    @Column(name = "NUM")
    private String num;

    @Column(name = "INIT")
    private String init;

    //gettter setter here

}

然后在您的实体中将此用作ID

public class BEntity implements Serializable{

    @Id
    private PKClass pkClass = null;

    @Column(name = "V_CNT")
    private Integer vcnt;

   //{{{some column omitted}}}//
}

public class AEntity implements Serializable{

    @Id
    private PKClass pkClass = null;

    @OneToOne
    @PrimaryKeyJoinColumns({
        @PrimaryKeyJoinColumn(name="NUM", referencedColumnName="NUM"),
        @PrimaryKeyJoinColumn(name="INIT", referencedColumnName="INIT")
    })
    private BEntity bEntity;
}
2020-06-20