提供了错误类型hibernate的id

时间:2012-05-29 18:55:35

标签: java hibernate annotations type-mismatch

我收到错误:

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 gen-code

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中运行代码时,它只能在代码中运行它导致问题......

3 个答案:

答案 0 :(得分:11)

看起来这是hibernate版本3.2.6中的一个缺陷,但仍未解决。遇到了JIRA

Hibernate支持多个@Id,但似乎在一对一映射下失败,建议解决这个问题的方法是使用单个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;
}

答案 1 :(得分:1)

我遇到了同样的问题。我有两个具有相同字段的独立PK类。所以我删除了一个PK并且只在所有者和子实体中使用了一个PK。这解决了这个问题。

答案 2 :(得分:0)

我遇到了同样的问题。

已启动情况

主表上的主键

        <composite-id name="id" class="model.E1Id">
        <key-property name="taxpayerId" type="int">
            <column name="TaxpayerID" />
        </key-property>
        <key-property name="year" type="int">
            <column name="Year" />
        </key-property>
    </composite-id>

详细信息表上的主键

 <composite-id name="id" class="model.E1dataFromTaxPayerFolderId">
        <key-property name="tid" type="int">
            <column name="tID" />
        </key-property>
        <key-property name="year" type="int">
            <column name="Year" />
        </key-property>
    </composite-id>

修复后 我更改了详细信息表composite-id的类,我设置了主表的类,如下所示:

        <composite-id name="id" class="model.E1Id">
        <key-property name="tid" type="int">
                            <column name="tID" />
                    </key-property>
        <key-property name="year" type="int">
            <column name="Year" />
        </key-property>
    </composite-id>

之后有必要(如果你有不同的名字)在你的主键类(在我的例子中是E1Id)中添加属性tid(使用getter和setter方法)