映射超类上的命名查询

映射超类上的命名查询

本文介绍了映射超类上的命名查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在映射的超类上声明一个NamedQuery,但我得到这个错误:

我们使用hibernate,



以下是代码:

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

似乎我不能使用我的mappedSuperClass查询,但我不明白为什么如果在JPA API中我发现这个:

我错了什么?



解决方案:我的解决方案是一个解决方法:我移动了子类的命名查询更改where子句适时。从我的角度来看,这给我提供了较少的代码可维护性,但我不能以另一种方式。

解决方案

@NameQuery c $ c> @MappedSuperClass 。过去Hibernate曾经发布过,导致此操作失败。



另一方面,查询本身不会工作,因为映射的超类不能被查询。在JPA 2.0规范中,使用以下词语:

这种限制类型是有意义的 - 映射超类的目的是共享映射在其他非相关实体之间,映射的超类不应该是实体继承层次结构的根。如果这样的查询工作,它纯粹是因为JPA供应商特定的扩展。


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

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:

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.

解决方案

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:

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.

这篇关于映射超类上的命名查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:21