Object with identifier does not belong to the specified subclass

I get a strange error in my application.
I am trying to get an entity list from a database (MySQL) using session.createCriteria().list(), but I am getting this org.hibernate.WrongClassException.

I looked at this error and I know what it means, but I do not know how to solve it in 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 tables are connected by these identifier columns, and at the last level, the table vtiger_accountscfhas 1 vtiger_accountshipadsand 1 vtiger_accountbillads. All tables have the same PK.
So I made my classes as follows (stubs):

@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 is my problem. When I do this:

getSession().createCriteria(VtigerAccountbillads.class).list();

I get an 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)

Due to the limitations of the project, I am not using spring or anything similar to setting up Hibernate and creating a session.

?

+4
1

, ?

, , , hibernate , , hibernate .

+1

All Articles