Hibernate: org.hibernate.WrongClassException, SINGLE_TABLE inheritance and discriminator Formula

I am using Hibernate 3.2.2 GA with HSQLDB 2.0 GA, and I have a class hierarchy similar to the following:

@Entity
@Table(name = "A_TABLE")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(value = "case when CODE IN (1, 2, 3, 4) then 'TypeB' 
when CODE   IN (5, 6, 7, 8) then 'TypeC' else NULL end")
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
public abstract class A{

 (...)

}


@Entity
@DiscriminatorValue("TypeB")
public class B extends A {

(...)

}


@Entity
@DiscriminatorValue("TypeC")
public class C extends A {

(...)

}

I am trying to execute the following HQL query that returns objects from classes B and C.

String hql = "from A a where a.someAttr = 3";
Query query = session.createQuery(hql);

return query.list();

However, I get the following error:

org.hibernate.WrongClassException: Object with id: 2 was not of the specified subclass: A (Discriminator: C      )

The strangest thing is that an object with id 2 is an instance of C ...

I was looking for this error, and I found some people who came across it, but not using InheritanceType.SINGLE_TABLEand DiscrimatorFormula. Has anyone encountered this problem?

+5
source share
4 answers

, (, persistence.xml). fooobar.com/questions/1071524/...

+4

, : , , :

, CHAR, VARCHAR. .

TRIM() ( @DiscriminatorFormula) ? Hibernate.

+1

, DTYPE WHERE. :

@Entity
@WHERE(clause = "DTYPE = 'B'")
public class B extends A {
 ...
}


@Entity
@WHERE(clause = "DTYPE = 'C'")
public class C extends A {
 ...
}
+1

, A Hibernate, , B C . , , .

, . , , , .

@ForceDiscriminator (A) , , .

Hibernate-specific. , JPA JPA.

EDIT:

, .

, sql, .

<property name="hibernate.show.sql" value="true" />

hibernate , .

Getting this sql with and without a suggestion about strength can give clues as to what strength does and why it doesn't work.

I can't think of anything else at the moment, except that NULLin your discriminator formula it looks a little risky.

0
source

All Articles