问题描述
org.hibernate.TypeMismatchException:为类BEntity提供了错误类型的id。预期:class BEntity,class 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;
// {{{某列省略}}} //
}
公共类AEntity实现可序列化{
@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查询:
字符串queryString =FROM AEntity AS A+
LEFT JOIN A.bEntityAS B+ $ 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生成代码
选择aentity0_.NUMBER为NUMBER4_0_,aentity0_.INITIAL为INITIAL4_0_,bentity_p1_.NUMBER为NUMBER5_1_,bentity_p1_.INITIAL为INITIAL5_1_,aentity0_.V_CNT为VCNT3_4_0_,aentity0_.EIN如EIN4_0_,aentity0_.TYP如TYP5_4_0_,aentity0_.TRUK_CNT如TRUK6_4_0_,bentity_p1_.TRUK_AXL_CNT如TRUK3_5_1_从USR.aentity aentity0_左外连接上aentity0_.NUMBER = bentity_p1_.NUMBER和aentity0_.INITIAL USR.bentity_PRIMARY bentity_p1_ = ((?,?,))
中的(aentity0_.INITIAL || aentity0_.NUMBER)($,$,$)中的(aentity0_.INITIAL)
和( (?,?,?)中的aentity0_.NUMBER)
和bentity_p1_.TRUK_AXL_CNT> 0
当我在SQL资源管理器中运行代码时,它只能在代码中运行它,因为这个问题...
是休眠版本3.2.6中的一个缺陷,它仍然没有解析编辑。遇到此。
@Id 由Hibernate支持,但似乎在一对一映射下失败,建议解决此问题的方法是使用单个CompositeKey,这意味着您创建一个PK class
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;
// getter setter here
}
然后在你的实体中使用它作为ID
public class BEntity implements Serializable {
@Id
private PKClass pkClass = null;
@Column(name =V_CNT)
private Integer vcnt;
// {{{某列省略}}} //
}
公共类AEntity实现可序列化{
@Id
private PKClass pkClass = null;
@OneToOne
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name =NUM,referencedColumnName =NUM),
@PrimaryKeyJoinColumn(name =INIT ,referencedColumnName =INIT)
})
private BEntity bEntity;
}
I am getting error:
org.hibernate.TypeMismatchException: Provided id of the wrong type for class BEntity. Expected: class BEntity, got class 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 query:
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
When I run the code in SQL Explorer it works only running it in code cause the issue...
Looks like this is a defect in hibernate version 3.2.6 which is still not resolved. Came across this JIRA.
Having multiple @Id
is supported by Hibernate but seems it fails under one to one mapping, suggested way of resolving this is to use single CompositeKey, which means you create a PK class
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
}
then in your Entity use this as the 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;
}
这篇关于提供了错误类型hibernate的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!