【发布时间】:2014-08-27 17:26:35
【问题描述】:
我的应用程序出现奇怪的错误。
我正在尝试使用session.createCriteria().list() 从数据库(MySQL)中检索实体列表,但我得到了这个org.hibernate.WrongClassException。
我已经查过这个错误,我知道它的含义,但我不知道如何根据我的上下文来解决它。
我有以下数据库结构:
CREATE TABLE vtiger_crmentity (
`crmid` int(19) NOT NULL
)
CREATE TABLE vtiger_account (
`accountid` int(19) NOT NULL DEFAULT 0
)
CREATE TABLE vtiger_accountscf (
`accountid` int(19) NOT NULL DEFAULT 0
)
CREATE TABLE vtiger_accoutshipads (
`accountaddressid` int(19) NOT NULL DEFAULT 0
)
CREATE TABLE vtiger_accountbillads (
`accountaddressid` int(19) NOT NULL DEFAULT 0
)
所以,快速解释一下,所有表都由这些 id 列链接,在最后一级,vtiger_accountscf 表有 1 个vtiger_accountshipads 和 1 个vtiger_accountbillads。所有表的 PK 相同。
所以我把我的课程变成了这样(存根):
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "vtiger_crmentity")
public class VtigerCrmentity {
@Id
@Basic(optional = false)
@Column(name = "crmid", nullable = false)
public Integer getId() {
return this.id;
}
}
@Entity
@PrimaryKeyJoinColumn(name = "accountid")
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "vtiger_account")
public class VtigerAccount extends VtigerCrmentity {
}
@Entity
@PrimaryKeyJoinColumn(name = "accountid")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "vtiger_accountscf")
public class VtigerAccountscf extends VtigerAccount {
}
@Entity
@PrimaryKeyJoinColumn(name = "accountaddressid")
@Table(name = "vtiger_accountbillads")
public class VtigerAccountbillads extends VtigerAccountscf {
}
@Entity
@PrimaryKeyJoinColumn(name = "accountaddressid")
@Table(name = "vtiger_accountshipads")
public class VtigerAccountshipads extends VtigerAccountscf {
}
这是我的问题。当我这样做时:
getSession().createCriteria(VtigerAccountbillads.class).list();
我遇到了异常:
org.hibernate.WrongClassException: Object with id: 11952 was not of the specified subclass: VtigerAccountbillads (loaded object was of wrong class class VtigerAccountshipads)
at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1391)
at org.hibernate.loader.Loader.getRow(Loader.java:1344)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:611)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
由于项目限制,我没有使用 spring 或任何类似的东西来配置 Hibernate 并创建会话。
我的映射错了吗?
【问题讨论】:
-
all the tables are linked by the these id columns-- 所以你的意思是说所有表的 id 值都相同?你也能澄清一下in the last level, the vtiger_accountscf table has 1 vtiger_accountshipads and 1 vtiger_accountbillads.是什么意思吗 -
是的,表中的 id 相同。我的意思是
vtiger_accounts与vtiger_accountbillads和vtiger_accountshipads结合在一起(这是给定帐户的地址)
标签: java mysql sql hibernate vtiger