Persist a List with order and duplicates in JPA/EclipseLink

无人久伴 提交于 2019-12-01 10:35:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15690347/persist-a-list-with-order-and-duplicates-in-jpa-eclipselink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!