1

I'm trying to declare a NamedQuery on a mapped superclass, but I obtain this error:

org.hibernate.hql.ast.QuerySyntaxException: VoipCall is not mapped [select v from VoipCall v where v.audioFile = :audioFile]

We use hibernate, but we prefer to use JPA standard notation.

Here is the code:

@MappedSuperclass
@NamedQueries(value = {
    @NamedQuery(name = "getVoipCallsForAudio", query = "select v from VoipCall v where v.audioFile = :audioFile")
})
public abstract class VoipCall implements Serializable {

It seems that I cannot use my mappedSuperClass in the query, but I don't understand why if in the JPA API I found this:

The NamedQuery annotation can be applied to an entity or mapped superclass.

Where am I wrong?

Thanks!!

SOLUTION: The solution for me was a workaround: I moved the named query on the subclasses changing the where clause opportunely. This from my point of view give me less maintainability of code, but I cannot do in another way.

Stefania
  • 641
  • 1
  • 12
  • 34
  • I think its because of the `abstract`. AFAIK, abstract classes cannot be instantiated and the `NamedQuery` you've provided creates instances of the `abstract` class `VoipCall`, which violated the rules of `abstract` keyword. – Rahul May 06 '13 at 10:14
  • I removed the abstract but I still obtain the same exception. – Stefania May 06 '13 at 10:24
  • I guess you're right about that! But I'm still unsure about executing a NamedQuery on an umapped class! May be you need to execute the named query on your sub-class, which would inturn fetch all the data of the MappedSuperClass too! That seems the way to go about it! – Rahul May 06 '13 at 11:00
  • The problem is that it gives me this exception when I deploy the web application in tomcat. It seems that is the declaration of the named query that it doesn't like. – Stefania May 06 '13 at 11:27
  • That's because all the named queries are compiled when your application starts up! Read this [answer](http://stackoverflow.com/a/7770977/2024761) for detailed info on that! – Rahul May 06 '13 at 11:32
  • I changed the call to the named query using the subclass as parameter `List voipCalls = em.createNamedQuery("getVoipCallsForAudio", VoipHistoryCall.class).setParameter("audioFile", audioFile).getResultList();` but still no change, the same exception still rises. It seems that the problem is the declaration of the named query on the mapped superclass, even if the api says that is possible. I've googled for some examples but I didn't find any, all the examples are on entities. – Stefania May 06 '13 at 11:53

1 Answers1

3

It is perfectly fine to have @NameQuery annotation in @MappedSuperClass. In past Hibernate used to had issue HHH-4364, which caused this to fail. Issue is fixed since years.

On the other hand query itself is not expected to work, because mapped superclass cannot be queried. In JPA 2.0 specification this is told with following words:

A mapped superclass, unlike an entity, is not queryable and must not be passed as an argument to EntityManager or Query operations.

This restriction kind of makes sense - purpose of mapped superclass is to share mappings between otherwise non-related entities, mapped superclass is not supposed to be root of the entity inheritance hierarchy. If such a query works, it is purely because of JPA vendor specific extension.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135