Named query on a Mapped Superclass

 ̄綄美尐妖づ 提交于 2020-01-11 07:12:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16396489/named-query-on-a-mapped-superclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!