如何反映“嵌套集” JPA中的模型 [英] How to reflect "Nested Sets" model in JPA

查看:283
本文介绍了如何反映“嵌套集” JPA中的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好使用嵌套集作为分层数据。但在此设计中,如果删除或插入一些数据,则应始终计算右侧和左侧节点。此外,你没有任何外国钥匙。

It is good to use Nested sets for the hierarchical data. But in this design you should calculate always the right and the left nodes if you delete or insert some data. Additionally you don't have any foreignkeys.

我如何用JPA反映这个模型?有可能吗?

How can i reflect this model with JPA?? Is it possible?

推荐答案

我认为你仍然希望PARENT_ID即使是RIGHT和LEFT,否则父/子查询会效率低下,或者我甚至不确定是否可行。

I think you would still want the PARENT_ID even with the RIGHT and LEFT, otherwise the parent/child queries would be inefficient, or I'm not even sure possible.

您可以在应用程序/模型中或通过PrePersist / PreUpdate / PreRemove事件更新的RIGHT和LEFT值。

The RIGHT and LEFT values you could update in your application/model, or through PrePersist/PreUpdate/PreRemove events.

另一种选择是使用爆炸表。为此,您只需要添加一个使用ManyToMany连接表的allChildren关系。当您将子项添加到任何节点时,您只需将子项添加到其所有父项。

Another option is to use an exploded table instead. For that you just need to add an allChildren relationship that uses a ManyToMany join table. When you add a child to any node, you just need to add the child to all of its parents.

另一个选项是在数据库中使用分层查询,例如Oracle中的CONNECT BY子句。

Yet another option is to just use a hierarchical query in your database, such as a CONNECT BY clause in Oracle.

这篇关于如何反映“嵌套集” JPA中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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