JPA支持@ManyToMany(mappedBy = ...)+ @OrderColumn吗? [英] Is @ManyToMany(mappedBy = ... ) + @OrderColumn supported by the JPA?

查看:578
本文介绍了JPA支持@ManyToMany(mappedBy = ...)+ @OrderColumn吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个映射如下的实体:

  @Entity 
@Table(name =Groups)
@IdClass(value = GroupId.class)
public class Group implements Serializable
{
@Id
@Column(name =round_id)
私人整数roundId;

@Id
@Column(name =ordinal_nbr)
private Integer ordinalNbr = 0;


}

它有一个组合键(round_id,ordinal_nbr)来表示一轮中组的顺序。现在想象一个包含实体的连接表通过自引用(从Group到Group)链接订购的组:

  CREATE TABLE GroupLinks 

parent_round_id INTEGER NOT NULL,
parent_ordinal_nbr SMALLINT NOT NULL,
child_round_id INTEGER NOT NULL,
child_ordinal_nbr SMALLINT NOT NULL,
PRIMARY KEY(parent_round_id,parent_ordinal_nbr,child_round_id,child_ordinal_nbr),
FOREIGN KEY(parent_round_id,parent_ordinal_nbr)参考组(round_id,ordinal_nbr),
FOREIGN KEY(child_round_id,child_ordinal_nbr)参考组(round_id,ordinal_nbr)
);

我知道可以映射 @ManyToMany + @JoinTable + @OrderColumn 为所属实体(无论我选择哪个):

  @ManyToMany 
@JoinTable(name =GroupLinks,joinColumns = {@JoinColumn(name =parent_round_id,referencedColumnName =round_id), @JoinColumn(name =parent_ordinal_nbr,referencedColumnName =ordinal_nbr)},inverseJoinColumns = {@JoinColumn(name =child_round_id,referencedColumnName =round_id),@JoinColumn(name =child_ordinal_nbr,referencedColumnName =ordinal_nbr )})
@OrderColumn(name =child_ordinal_nbr)
私人列表< Group>儿童;

问题:

对于拥有的方面,是否支持将 @ManyToMany(mappedBy = ...) @OrderColumn <

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' parent_ordinal_nbr)
私人列表< Group>父母;

JPA 2规范是定义允许还是拒绝这个?

感谢




更新1: b

以上基本上是一个图结构。以下是一个例子:





GroupLinks 表包含盒装实体。当仅查看B2 Group 实体时,父母列表将包含(a,1 ,b,2)(a,2,b,2)。至于 children 列表,它将包含(b,2,c,1)(b,2,c,2)(b,2,c,3)



正如你可以看到 B2 列表中的实体不会发生冲突,所以 @OrderColumn 对于两个关系 父母儿童 。但是,这里的实践(JPA)是什么?



请注意,仅仅在Hibernate和/或EclipseLink上进行尝试并不能真正回答JPA 2规范或JPA兼容的提供商应该或必须支持这种情况。






更新2:



尝试Hibernate的上述映射会导致以下映射异常:

 引起通过:org.hibernate.MappingException:在集合映射中的重复列:com.kawoolutions.bbstats.model.Group.parents column:parent_ordinal_nbr $ b $在org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:340) 
在org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:363)
在org.hibernate.mapping.Collection.validate(Collection.java:320)
在org.hibernate .mapping.IndexedCollection.validate(IndexedCollection.java:89)
org.hibernate.cfg.Configuration.validate(Configuration.ja va:1291)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1729)
at org.hibernate.ejb.EntityManagerFactoryImpl。< init>(EntityManagerFactoryImpl.java:84)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
... 9 more

parent_ordinal_nbr 移除 @OrderColumn 会导致 children 关系。看起来Hibernate不喜欢在外键中使用的order列。






更新3:

使用GlassFish上的EclipseLink进行测试...这样做,没有映射异常。



引发了两个后续问题:


  1. JPA不允许外键列?我为什么他们不应该简单地工作......

  2. 这是一个Hibernate错误吗?


解决方案

javadoc或OrderColumn 包含您要查找的信息:


OrderColumn注释是在
关系的边上指定的引用要订购的集合。
order列不可见作为实体或
可嵌入类的一部分。



OrderBy注释应该用于排序可见为
持久状态并由应用程序维护。在指定OrderColumn时,不会使用OrderBy
注释。

OrderColumn用于向连接表中添加额外的列,用于维护列表的顺序。在javadoc中提到的order列不是实体状态的一部分。在你的情况下,你似乎想要通过其持久属性之一来排列列表中的元素。在这种情况下,您必须使用OrderBy批注(或者在返回之前有一个对列表进行排序的getter)。


I have an entity mapped as following:

@Entity
@Table(name = "Groups")
@IdClass(value = GroupId.class)
public class Group implements Serializable
{
    @Id
    @Column(name = "round_id")
    private Integer roundId;

    @Id
    @Column(name = "ordinal_nbr")
    private Integer ordinalNbr = 0;

    ...
}

It has a composite key (round_id, ordinal_nbr) to indicate the order of groups in a round.

Now imagine a join table containing entities that link the ordered groups via a self reference (from Group to Group):

CREATE TABLE GroupLinks
(
  parent_round_id INTEGER NOT NULL,
  parent_ordinal_nbr SMALLINT NOT NULL,
  child_round_id INTEGER NOT NULL,
  child_ordinal_nbr SMALLINT NOT NULL,
  PRIMARY KEY (parent_round_id, parent_ordinal_nbr, child_round_id, child_ordinal_nbr),
  FOREIGN KEY (parent_round_id, parent_ordinal_nbr) REFERENCES Groups (round_id, ordinal_nbr),
  FOREIGN KEY (child_round_id, child_ordinal_nbr) REFERENCES Groups (round_id, ordinal_nbr)
);

I know it's possible to map @ManyToMany + @JoinTable + @OrderColumn for the owning entity (whichever I choose):

@ManyToMany
@JoinTable(name = "GroupLinks", joinColumns = {@JoinColumn(name = "parent_round_id", referencedColumnName = "round_id"), @JoinColumn(name = "parent_ordinal_nbr", referencedColumnName = "ordinal_nbr")}, inverseJoinColumns = {@JoinColumn(name = "child_round_id", referencedColumnName = "round_id"), @JoinColumn(name = "child_ordinal_nbr", referencedColumnName = "ordinal_nbr")})
@OrderColumn(name = "child_ordinal_nbr")
private List<Group> children;

Question:

For the owned side, is it supported to map the @ManyToMany(mappedBy = ...) inverse relationship with an @OrderColumn, too?

@ManyToMany(mappedBy = "parents")
@OrderColumn(name = "parent_ordinal_nbr")
private List<Group> parents;

Does the JPA 2 spec define to allow or deny this?

Thanks


Update 1:

The above is essentially a graph structure. Here's an example:

The GroupLinks table contains the boxed entities. When looking at the B2 Group entity only, the parents list will contain (a,1,b,2) and (a,2,b,2). As for the children list, it will contain (b,2,c,1), (b,2,c,2), and (b,2,c,3).

As you can see the entities in the lists of B2 won't collide, so an @OrderColumn should work okay for both relationships parents and children - at least in theory. But what's the practice here (JPA)?

Note, that simply trying it on Hibernate and/or EclipseLink doesn't really answer the question whether the JPA 2 spec or a JPA-compatible provider should or must support this scenario.


Update 2:

Trying the above mappings on Hibernate results in the following mapping exception:

Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: com.kawoolutions.bbstats.model.Group.parents column: parent_ordinal_nbr
    at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:340)
    at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:363)
    at org.hibernate.mapping.Collection.validate(Collection.java:320)
    at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:89)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1291)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1729)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
    ... 9 more

Removing the @OrderColumn for parent_ordinal_nbr results in the same exception for the children relationship. It looks like Hibernate doesn't like order columns which are also used in foreign keys.


Update 3:

Tested with EclipseLink on GlassFish... this works, no mapping exceptions.

This raises two followup questions:

  1. Does the JPA disallow foreign key order columns? It doesn't make sense to me why they shouldn't simply work...
  2. Is this a Hibernate bug?

解决方案

The javadoc or OrderColumn has the information you're looking for:

The OrderColumn annotation is specified on the side of the relationship that references the collection that is to be ordered. The order column is not visible as part of the state of the entity or embeddable class.

The OrderBy annotation should be used for ordering that is visible as persistent state and maintained by the application. The OrderBy annotation is not used when OrderColumn is specified.

OrderColumn is used to add an additional column to the join table, used to maintain the order of the list. The order column, as mentioned in the javadoc, is not part of the entity state. In your case, it seems you want to order the elements in the list by one of their persistent property. In that case, you must use the OrderBy annotation (or have a getter that sorts the list before returning it).

这篇关于JPA支持@ManyToMany(mappedBy = ...)+ @OrderColumn吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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