Question

I'm developing a web application with Netbeans (Maven Web Application) using Spring MVC and Hibernate. I have an abstract class (User) with multiple concrete classes (Parent, Child, etc.). I chose for a table-per-hierarchy construction so that I have 1 tables with all the objects of these concrete classes. But I can't find a way to get all the objects of a certain concrete class. I'm getting following error:

java.lang.IllegalArgumentException: Type specified for TypedQuery [domain.Parent] is incompatible with query return type [class domain.User]

Anyone who knows how I can cast these objects?

Was it helpful?

Solution

You should have a discriminator columen in your BD, and a mapping like that:

@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.INTEGER)
public abstract class User {

and:

@DiscriminatorValue("1")
public class Parent extends User {

Then you can use the attribute class in your queries. For example:

getSession().createCriteria(User.class).add(Restrictions.eq("class", 1))

or in HQL:

FROM User WHERE class = 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top