views:

157

answers:

3

My application is a store selling fishes, aquariums etc. I want to get a list of top 10 items among all the items based on sales count. I use the following class:

@MappedSuperclass
@NamedQueries({
    @NamedQuery(name="getTopItems",query="SELECT x FROM FishStoreItem x ORDER BY x.salescnt DESC, x.title DESC")
})
public abstract class FishStoreItem 
       extends DomainSuperClass implements Serializable {
......
}

Problem is in the following exception:

Exception [EclipseLink-8034] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException Exception Description: Error compiling the query [getTopItems: SELECT x FROM FishStoreItem x ORDER BY x.salescnt DESC, x.title DESC]. Unknown entity type [FishStoreItem].

Same code works fine with Apache OpenJpa 2.0.0, but fails with EclipseLink ver 2.1.0, 2.0.1, 1.0.

P.S. I've already found that solution for Hibernate, but I want to be sure that it is impossible for EclipseLink too.

+1  A: 

A mapped superclass is not an entity and does not allow querying, persisting, or relationships to the superclass (see Mapped Superclasses).

This is confirmed in Apache OpenJPA 2.0 User's Guide:

1.3. Mapped Superclass

A mapped superclass is a non-entity class that can define persistent state and mapping information for entity subclasses. Mapped superclasses are usually abstract. Unlike true entities, you cannot query a mapped superclass, pass a mapped superclass instance to any EntityManager or Query methods, or declare a persistent relation with a mapped superclass target. You denote a mapped superclass with the MappedSuperclass marker annotation.

...

Note

OpenJPA allows you to query on mapped superclasses. A query on a mapped superclass will return all matching subclass instances. OpenJPA also allows you to declare relations to mapped superclass types; however, you cannot query across these relations.

So, while OpenJPA allows your query, this is beyond JPA. Don't expect it to work with any JPA provider.

Pascal Thivent
+1  A: 

That code also works fine with DataNucleus as a JPA implementation. Being able to query for instances of a mapped superclass, whilst being beyond the JPA spec, is a very reasonable thing to require, and we believe in providing such things. After all the persistence provider knows which subclasses there are of that mapped superclass so makes little sense to not allow the user such a facility.

DataNucleus
+1  A: 

Can you confirm that you simply want a separate query run for every entity subclass and the results aggregated together?

This is similar to TABLE_PER_CLASS querying.

I have built an extension for EclipseLink in the past that can handle such querying and we can investigate adding it to the project if there is community interest and we can nail down the requirements.

Doug

Doug Clarke