递归JPA查询? [英] Recursive JPA query?

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

问题描述

JPA 2是否有任何运行递归查询的机制?

Does JPA 2 have any mechanism for running recursive queries?

这是我的情况:我有一个实体E,它包含一个整数字段x。它也可能有类型E的子节点,通过@OneToMany映射。我想做的是通过主键找到一个E,并获得它的x值以及它所有后代的x值。有没有办法在单个查询中执行此操作?

Here's my situation: I have an entity E, which contains an integer field x. It also may have children of type E, mapped via @OneToMany. What I'd like to do is find an E by primary key, and get its value of x, along with the x values of all its descendants. Is there any way to do this in a single query?

我正在使用Hibernate 3.5.3,但我不希望对Hibernate API有任何明确的依赖关系。

I'm using Hibernate 3.5.3, but I'd prefer not to have any explicit dependencies on Hibernate APIs.

编辑:根据这个项,Hibernate 有这个功能,或者至少它没有在3月份。所以JPA似乎不太可能拥有它,但我想确定。

According to this item, Hibernate does not have this feature, or at least it didn't in March. So it seems unlikely that JPA would have it, but I'd like to make sure.

推荐答案

使用简单的邻接模型其中每一行包含对其父项的引用,该引用将引用同一表中的另一行,但与JPA不能很好地协作。这是因为JPA不支持使用Oracle CONNECT BY子句或SQL标准WITH语句生成查询。如果没有这两个条款中的任何一个,它就不可能使邻接模型变得有用。

Using the simple Adjacency Model where each row contains a reference to its parents which will refer to another row in same table doesn't co-operate well with JPA. This is because JPA doesn't have support for generating queries using the Oracle CONNECT BY clause or the SQL standard WITH statement. Without either of those 2 clauses its not really possible to make the Adjacency Model useful.

然而,还有其他几种可以应用于此问题的方法来解决这个问题。问题。第一个是物化路径模型。这是将节点的完整路径展平为单个列的位置。表定义扩展如下:

However, there are a couple of other approaches to modelling this problem that can applied to this problem. The first is the Materialised Path Model. This is where the full path to the node is flattened into a single column. The table definition is extended like so:

CREATE TABLE node (id INTEGER,
                   path VARCHAR, 
                   parent_id INTEGER REFERENCES node(id));

要插入节点树看起来像:

To insert a tree of nodes looks some thing like:

INSERT INTO node VALUES (1, '1', NULL);  -- Root Node
INSERT INTO node VALUES (2, '1.2', 1);   -- 1st Child of '1'
INSERT INTO node VALUES (3, '1.3', 1);   -- 2nd Child of '1'
INSERT INTO node VALUES (4, '1.3.4', 3); -- Child of '3'

因此要获得Node'1'及其所有子节点的查询是:

So to get Node '1' and all of its children the query is:

SELECT * FROM node WHERE id = 1 OR path LIKE '1.%';

要将此映射到JPA,只需将path列设为持久对象的属性即可。但是,您必须进行簿记才能使路径字段保持最新状态。 JPA / Hibernate不会为您执行此操作。例如。如果将节点移动到另一个父节点,则必须更新父节点引用并从新父节点对象确定新路径值。

To map this to JPA just make the 'path' column an attribute of your persistent object. You will however have to do the book-keeping to keep the 'path' field up to date. JPA/Hibernate won't do this for you. E.g. if you move the node to a different parent you will have to update both the parent reference and determine the new path value from the new parent object.

调用另一种方法嵌套集模型,这有点复杂。可能最好的由其创始人描述(而不是由我逐字添加) )。

The other approach is called the Nested Set Model, which is bit more complex. Probably best described by its originator (rather than added verbatim by me).

还有第三种方法叫做嵌套间隔模型,但这很大程度上依赖于存储过程来实现。

There is a third approach called Nested Interval Model, however this has a heavy reliance of stored procedures to implement.

SQL的艺术

这篇关于递归JPA查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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