0

I have the following entities:

@Entity
@Table(name = "TABLENAME")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator", discriminatorType=DiscriminatorType.STRING)
public class Base { ... }

@Entity
@DiscriminatorValue("value1")
public class SubEntity1 extends Base {}

@Entity
@DiscriminatorValue("value2")
public class SubEntity2 extends Base {}

I need to make a query without knowing the actual type of the result. Something like:

select b from Base b where b.discriminator=? and ...

I get this exception: Object with id: 31 was not of the specified subclass: packagename.Base

In this case the query result would contains only instances of the same sub-entity, but I have other queries where the result would be "mixed".

Is there any way to make a query like these?

Andrea Polci
  • 1,011
  • 13
  • 27

1 Answers1

1

You are trying to retrieve the root level parent Entity. If so, you need to add @DiscriminatorValue(value = "your_base") in Base entity class. Even there is no sub class of its, it will be OK. It is already post in here.

Community
  • 1
  • 1
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131