Pregunta

I have three classes that are subclasses of the abstract superclass Automobile. I'm using the single table inheritance model and a @MappedSuperclass for the automobile class. The subclasses are Car, Truck, and Van.

I want to query the automobile class, but have the different subclasses returned. I've written a couple queries and done some research, but it seems like it is not possible to query against MappedSuperclasses. I have also tried to do NativeSqlQueries, but I can't seem to be able to figure out how to specifiy multiple ResultSetMappings.

Is there anyway to accomplish this?

¿Fue útil?

Solución

You cannot use a mapped-superclass in the query. If you want to use Automobile in the query, don't mark it as mapped-superclass, instead mark it as an entity.

Following is from the JPA 2.0 Spec, second paragraph is what's relevant in your case -

2.11.2 Mapped Superclasses

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.

A mapped superclass, unlike an entity, is not queryable and must not be passed as an argument to EntityManager or Query operations. Persistent relationships defined by a mapped superclass must be unidirectional.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top