Hibernate ManyToMany更新时出现Join table问题 [英] Hibernate ManyToMany with Join table problems on update

查看:106
本文介绍了Hibernate ManyToMany更新时出现Join table问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使ManyToMany关联对所有CRUD操作都起作用.我有两个实体:PlacesEvents.

I am trying to make a ManyToMany association work for all CRUD operations. I have two entities : Places and Events.

地点可以容纳多个事件,并且一个事件可以在多个地方发生.

Places can hold multiple events, and an events can take place in multiple places.

第一种情况下我有

在PlaceDto类中

In class PlaceDto

@ManyToOne(  
    targetEntity=EventDto.class,  
    cascade = { CascadeType.PERSIST, CascadeType.MERGE })  
@JoinTable(  
    name = "EVENTS_PLACES",  
    joinColumns = { @JoinColumn(name = "PLACE_ID") },  
    inverseJoinColumns = { @JoinColumn(name = "EVENT_ID") })  
private List<EventDto> events;

在PlaceDto类中:

In class PlaceDto:

@JoinTable(name = "EVENTS_PLACES", joinColumns = @JoinColumn(name = "EVENT_ID"), inverseJoinColumns = @JoinColumn(name = "PLACE_ID"))
private List<PlaceDto> places;

在这种情况下,在更新地点时,该地点与其事件之间的链接已删除
使用DELETE FROM EVENTS_PLACES where ...语句

in this case on updating a place the link between the place and its event was erased
with DELETE FROM EVENTS_PLACES where ... statement

第二种情况
因此,在阅读了一些文档之后,我将PlaceDto更改为

Second case
So after reading some docs, I changed PlaceDto to

@ManyToMany (
   mappedBy = "events",  
   cascade = { CascadeType.PERSIST, CascadeType.MERGE },  
   fetch = FetchType.LAZY,  
   targetEntity = FundDto.class)  
private List<PlaceDto> places;

更新地点时,一切似乎都很好, 但是当我尝试创建活动时,它也会尝试创建一个地方
=>会导致主键冲突.

When updating a place everything seems to be fine, but when I try to create an event, it tries also to create a place
=> which leads to a primary key violation.

我使用eclipse覆盖了hashcode()和equals().

I have hashcode() and equals() overridden using eclipse.

感谢您的帮助.

请毫不犹豫地指向我显示和说明完全正常运行且经过测试的ManyToMany关系的页面.

Please do not hesitate to point me to a page where a fully working and tested ManyToMany relation is shown and explained.

克里斯托弗

推荐答案

这是一个很好的只需尝试将他们所拥有的内容复制到您自己的代码中,它就可以工作. 如果还是不能,请发布两个类的完整清单以及导致错误的代码,也许我可以看到问题所在.

Just try to copy what they have there into your own code and it should work. If it still doesn't, please post full listings for two classes and the code that causes the error and maybe I can see what the problem is.

这篇关于Hibernate ManyToMany更新时出现Join table问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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