java 带有 id 的对象不属于指定的子类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25533449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object with id was not of the specified subclass
提问by Alberson Melo
I'm getting a weird error on my application.
I'm trying to retrieve an list of entity from database (MySQL) with session.createCriteria().list()
but I'm getting this org.hibernate.WrongClassException
.
我的应用程序出现奇怪的错误。
我正在尝试从数据库 (MySQL) 中检索实体列表,session.createCriteria().list()
但我得到了这个org.hibernate.WrongClassException
.
I have looked up this error and I know what it means, but I don't know how to solve it on my context.
我查了这个错误,我知道它意味着什么,但我不知道如何在我的上下文中解决它。
I have the following database structure:
我有以下数据库结构:
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
)
So, quickly explaining, all the tables are linked by the these id columns, and in the last level, the vtiger_accountscf
table has 1 vtiger_accountshipads
and 1 vtiger_accountbillads
. All the tables have the same PK.
So I made my classes like this (stubs):
因此,快速解释一下,所有表都由这些 id 列链接,在最后一层,vtiger_accountscf
表有 1vtiger_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 {
}
And here's my problem. When I do:
这是我的问题。当我做:
getSession().createCriteria(VtigerAccountbillads.class).list();
I'm getting the exception:
我收到异常:
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)
By project limitations, I'm not using spring or nothing similar to configure the Hibernate and create the session.
由于项目限制,我没有使用 spring 或类似的东西来配置 Hibernate 和创建会话。
Is my mapping wrong?
我的映射错了吗?
采纳答案by Chaitanya
Can you tell what data records you have in DB?
你能说出你在数据库中有哪些数据记录吗?
It seems the id's between different tables has same value, so when hibernate is trying to load an entity with a specific id and if another entity with same id is already present in memory then hibernate is complaining about this issue.
似乎不同表之间的 id 具有相同的值,因此当 hibernate 尝试加载具有特定 id 的实体时,如果内存中已经存在具有相同 id 的另一个实体,则 hibernate 会抱怨这个问题。
回答by Sal
Additionally to @Chaitanya's answer check when using a persistence.xml that there exists also a mapping for that entity.
除了@Chaitanya 在使用persistence.xml 时的答案检查之外,还存在该实体的映射。
回答by BenediktGansinger
I ran into the same exception, but for a completely different reason.
我遇到了同样的异常,但出于完全不同的原因。
We have entities A, B and C. B and C extend A, A is abstract and we used SINGLE_TABLE inheritance.
我们有实体 A、B 和 C。B 和 C 扩展了 A,A 是抽象的,我们使用了 SINGLE_TABLE 继承。
We also have entities X, Y and Z with the same properties (Y and Z extend X, X is abstract and we used SINGLE_TABLE inheritance).
我们还有具有相同属性的实体 X、Y 和 Z(Y 和 Z 扩展 X,X 是抽象的,我们使用 SINGLE_TABLE 继承)。
X has a field of A and is of generic Type A:
X 有一个字段 A 并且是通用类型 A:
public abstract class X<T extends A> {
@ManyToOne
T field;
}
Y and Z specify the types of B and C respectively:
Y 和 Z 分别指定 B 和 C 的类型:
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "Y")
public abstract class Y extends X<B> {
}
We had other classes implement X without specifying a concrete type (keeping < T extends A >) - even when there are no Y or Z in the database (which means Hibernate never has to load any and should actually never create objects Y or Z) we would (sometimes) get a org.hibernate.WrongClassException. Our solution was to remove the concrete types B and C from classes Y and Z and use casts in our code.
我们让其他类在没有指定具体类型的情况下实现 X(保持 < T extends A >) - 即使数据库中没有 Y 或 Z (这意味着 Hibernate 永远不必加载任何对象,实际上永远不应该创建对象 Y 或 Z)我们(有时)会得到一个 org.hibernate.WrongClassException。我们的解决方案是从 Y 和 Z 类中删除具体类型 B 和 C,并在我们的代码中使用强制转换。
Additional note: this seemed to work fine for some years, but popped up after an architectural change where neither hibernate configuration nor any of the classes were changed (we just renamed some packages) and then didn't go away until we removed the concrete generic types.
附加说明:这似乎在几年内运行良好,但是在架构更改后突然出现,其中休眠配置和任何类都没有更改(我们只是重命名了一些包)然后直到我们删除了具体的泛型才消失类型。