简体   繁体   English

JPA实体两次映射到另一个类,一次删除

[英]JPA entity mapped twice to another class, delete once

I have a following problem, have the device: 我有以下问题,请使用设备:

@OneToMany(mappedBy = 'sender', cascade = CascadeType.ALL, fetch = FetchType.EAGER) Set<Gift> sentGifts = []
@OneToMany(mappedBy = 'receiver', cascade = CascadeType.ALL, fetch = FetchType.EAGER) Set<Gift> receivedGifts = []

And gift: 和礼物:

@ManyToOne Device sender
@ManyToOne Device receiver

Problem is that I cannot understand how to properly delete gift. 问题是我不明白如何正确删除礼物。

I can do > gift.sender = null > save(gift) > gift.receiver = null > save(gift) > ... 我可以做> gift.sender = null> save(礼物)> gift.receiver = null> save(礼物)> ...

But i believe that this is not the correct way to do this, maybe cascade should be specified somehow in another way? 但是我认为这不是正确的方法,也许应该以另一种方式指定级联?

Thanks! 谢谢!

You should make updates to the devices referencing the gift before removing it, as otherwise those references will still exist in your object model and might cause the gift to be resurrected. 您应在删除礼物之前对引用礼物的设备进行更新,否则这些引用将仍然存在于您的对象模型中,并可能导致礼物被复活。 Removing the gift from the senders set of sentGifts and the receivers set of receivedGifts, then save both, then remove the gift should be enough. 从发件人的sendGifts组和接收者组的ReceivedGifts组中删除礼物,然后保存两者,然后删​​除礼物就足够了。 The changes to the sender and receiver device won't result in database changes; 发送方和接收方设备的更改不会导致数据库更改; they are only to keep your entities inline with what you want in the database, but should be done as entities can be cached in JPA. 它们只是使您的实体与数据库中所需的内容保持内联,但是应该这样做,因为可以在JPA中缓存实体。 There is no reason to null out receiver or sender on the gift before deleting the gift. 在删除礼物之前,没有理由取消礼物的接收者或发送者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM