带有继承的JPA原生查询实体 [英] JPA Native Query for Entity with Inheritance

查看:775
本文介绍了带有继承的JPA原生查询实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体类和基于该实体的子类:

  @Entity 
@Inheritance =继承类型.TABLE_PER_CLASS)
公共类A

  @Entity 
公共类B延伸A

我需要发出仅在基类(A)上使用存储过程的本机查询。如果我尝试如下:

  entityManager.createNativeQuery(select * from A a where procedure(f),A。 class).getResultList()

我得到一个关于在结果集中找不到列clazz_ 。我假设JPA提供程序添加此列以区分基类和扩展类。我可以通过显式地添加clazz列和子类中的所有字段来解决此问题:

  entityManager.createNativeQuery( select *,1 as clazz_,null as prop1,null as prop2 from A a where procedure(f),A.class).getResultList()

其中prop1和prop2是子类B的属性。但是,这看起来像是一种不必要的破解,如果子类B发生更改,则容易出现维护问题。



我的问题是:如何在定义了继承的实体上使用存储过程进行查询?


16.1.6。处理继承



查询
映射为
继承的一部分的实体的原生SQL查询必须包含所有
属性基类和所有
的子类。


因此,如果您想使用Native查询,喜欢这个。关于子类B更改的问题,也许稍微不太麻烦的方法是尝试在共享ID属性上使用LEFT OUTER JOIN语法:

  entityManager.createNativeQuery(select a。*,b *,1 as clazz_,from A a LEFT OUTER JOIN B b on id = a.id where procedure(f),A.class)。 getResultList()

如果您添加或删除,那么您将始终从B中获取所有属性一些。

I have an entity class and a subclass based on that entity:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class A

and

@Entity
public class B extends A

I need to issue a native query that uses a stored procedure on the base class (A) only. If I attempt it as follows:

entityManager.createNativeQuery("select * from A a where procedure(f)",A.class).getResultList()

I get an error regarding "The column clazz_ was not found in the ResultSet". I assume that the JPA provider adds this column in order to discriminate between the base class and the extended class. I can work around this problem by explicitly adding the clazz column and all of the fields from the subclass:

entityManager.createNativeQuery("select *,1 as clazz_,null as prop1,null as prop2 from A a where procedure(f)",A.class).getResultList()

where "prop1" and "prop2" are properties of the subclass B. However, this seems like an unnecessary hack and is prone to maintenance problems if the subclass B changes.

My question is: How can I query using a stored procedure on an entity that has inheritance defined on it?

解决方案

As you've probably seen, the Hibernate team hasn't put a lot of work into defining how you do this.. the documentation simply states:

16.1.6. Handling inheritance

Native SQL queries which query for entities that are mapped as part of an inheritance must include all properties for the baseclass and all its subclasses.

So if you want to use Native queries it looks like you're stuck doing something like this. Regarding the concern about the subclass B changing, perhaps a slightly less onerous way of implementing this would be to try using LEFT OUTER JOIN syntax on the shared ID property:

entityManager.createNativeQuery("select a.*, b*, 1 as clazz_, from A a LEFT OUTER JOIN B b on id = a.id where procedure(f)",A.class).getResultList()

That way you'll always get all of the properties from B if you add or remove some.

这篇关于带有继承的JPA原生查询实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆