在JPA / EclipseLink中保存具有顺序和重复的列表 [英] Persist a List with order and duplicates in JPA/EclipseLink

查看:180
本文介绍了在JPA / EclipseLink中保存具有顺序和重复的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有两个实体 Entity1 Entity2
Entity1 包含此位代码:

  @OneToMany (cascade = CascadeType.PERSIST)
@OrderColumn
List< Entity2> e = new LinkedList< Entity2>();

Entity2 相关。我想添加一些实例到列表 e ,最重要的是,我想添加一些重复(相同的引用)两次或更多的列表。仍然我希望JPA坚持两个,我想让JPA坚持列表的顺序。



到目前为止,这是有效的,如果我只添加了唯一的项目。如果我添加一个重复,JPA实际上想要持久保存正确的数据(ID od Entity1,Entity2的ID和顺序),但由于关系表的主键只有两个ID,JPA抛出一个重复的主键错误。 / p>

我如何解决这个问题?

解决方案

EclipseLink目前不支持OneToMany关系的重复。重复支持(基本)ElementCollection映射。



请投票支持错误,
https://bugs.eclipse.org/bugs/show_bug.cgi?id=256978



最好的解决方案是将连接表映射到另一个实体,并使用到目标的1-1定义一个1-m。


I have basically two Entities Entity1 and Entity2. Entity1 contains this bit of Code:

@OneToMany(cascade=CascadeType.PERSIST)
@OrderColumn
List<Entity2> e = new LinkedList<Entity2>();

and Entity2 contains some stuff, but nothing relevant. I want to add some instances to the list e, and most importantly i want to add some duplicates (the same reference) twice or more to the list. Still i want JPA to persist both and i want JPA to persist the Order of the list.

So far this works, if i add only unique items. If i add a duplicate, JPA actually wants to persist the correct data (ID od Entity1, ID of Entity2 and the order) but since the Primary key of the relationship table is only the two IDs, JPA throws an duplicate primary key error.

How can i solve this?

解决方案

EclipseLink does not currently support duplicates for OneToMany relationships. Duplicate are support for (basic) ElementCollection mappings.

Please vote for bug, https://bugs.eclipse.org/bugs/show_bug.cgi?id=256978

The best solution is to map the join table to another entity and define a 1-m to it with a 1-1 to the target.

这篇关于在JPA / EclipseLink中保存具有顺序和重复的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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