如何在JPA中为自己定义多对多? [英] How to define many-to-many to itself in JPA?

查看:139
本文介绍了如何在JPA中为自己定义多对多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JPA中定义此SQL架构:

I'm trying to define this SQL schema in JPA:

TABLE event (id INT)
TABLE chain (predecessor INT, successor INT)

换句话说,每个事件都有许多后继者,事件本身。我试图在JPA中这样做:

In other words, every event has a number of successors, which are events themselves. I'm trying to do it this way in JPA:

@Entity
public class Event {
  @Id Integer id;
  @ManyToMany(cascade = CascadeType.PERSIST)
  @JoinTable(
    name = "chain",
    joinColumns = @JoinColumn(name = "successor"),
    inverseJoinColumns = @JoinColumn(name = "predecessor")
  )
  private Collection<Event> predecessors;
}

@Entity
public class Chain {
  @Id Integer id;
  @ManyToOne(cascade = CascadeType.PERSIST)
  @JoinColumn(name = "predecessor")
  private Event predecessor;
  @ManyToOne(cascade = CascadeType.PERSIST)
  @JoinColumn(name = "successor")
  private Event successor;
}

是否正确?

推荐答案

通常情况下,既不会使用JoinTable定义ManyToMany,也会将连接表单独定义为自己的实体。连接表通常不是实体,它们只是连接表,提供商在引擎盖下管理它们。当你改变一个或另一个时,你正在为自己创造很多麻烦,只要正确地保持应用程序的内存状态。 (例如,如果你想使用L2缓存,那么这是必要的。)

Normally one would not both define a ManyToMany with a JoinTable and then also separately define the join table as its own Entity. Join tables aren't Entities normally, they're just join tables and the provider manages them under the hood. You're creating a lot of headaches for yourself as far as properly maintaining in memory state of the application when you change one or the other. (Which is necessary if, for example, you want to use L2 caching.)

所以,任何一个工作正常,合并,它们有点可能性。通常,如果您将Chain定义为实体,那么您只需要在事件上有一个Chain列表。不会将其重新定义为事件上的JoinTable。这有意义吗?

So, either one works fine, combined, they are sort of oddsauce. Usually if you defined Chain as an entity, you would just have a list of Chain on the Event. Not also redefine it as a JoinTable on Event. Does that make sense?

(因为它目前是严格定义的,如果您尝试通过Event上的集合进行更改,它将会中断,除非该ID是生成的数据库序列。)

(and as it is currently strictly defined, it will break if you try to make changes through the collection on Event unless that ID is a database generated sequence.)

编辑:类似这样的事情 -

something like this -

@Entity
public class Event {
  @Id Integer id;

  @OneToMany(cascade = CascadeType.PERSIST, mappedBy="successor")
  private Collection<Chain> predecessorChains;
}

只要您意识到,您最初写的内容就可以工作收集和LT;事件>前辈本质上是只读的,如果你试图对其进行L2缓存,它会变得很好。您在其上放置 CascadeType 这一事实使您希望能够添加和删除事件,这将在hibernate尝试执行非法时爆炸SQL。

What you wrote originally can be made to work as long as you realize that the Collection<Event> predecessors is inherently read only and will get fubared if you try to L2 cache it. The fact that you put a CascadeType on it makes one thing that you wanted to be able to add and remove Events to/from that, which will explode when hibernate tries to execute illegal SQL.

这篇关于如何在JPA中为自己定义多对多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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