gpt4 book ai didi

java - "cascading" hibernate 实体监听器?

转载 作者:行者123 更新时间:2023-11-30 09:56:30 26 4
gpt4 key购买 nike

使用 hibernate 3.2.4.sp1 我有两个实体用户和联系信息

一个 User 有一个相关的 ContactInfo,关系是单向的,即 ContactInfo 不知道它属于哪个用户(也不应该因为它在其他关系中被重用)

User 实体有一个实体监听器,防止除实际用户本身之外的其他用户在数据库中更新它。这很好用。问题是一个用户可以更新另一个用户 ContactInfo。我想在 ContactInfo 上的保存从用户级联时包括运行实体监听器,这可能吗?

目前的关系

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Cascade( { org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@JoinColumn(name = "CONTACT_PERSON", nullable = false)
public ContactPerson getContactInfo() {
return contactInfo;
}

public void setContactInfo(ContactPerson contactInfo) {
this.contactInfo = contactInfo;
}

当前:实体监听器:

public class UserListener extends EntityListener {

@PreUpdate
@PrePersist
@PreRemove
public void checkWriteAuthorization(User user) throws IllegalUserAccessException {
//access rules here
}
}

我想我要找的是“@PreCascadeChanges”之类的东西

实际的存储调用总是在实体 User 上,更新被级联到 ContactInfo

最佳答案

I would like to include running the entity listener when a save on a ContactInfo is cascaded from a user, is that possible?

我不这么认为,所以你必须在回调 @PreUpdate@PrePersist@PreRemove 中执行你的检查> 就像 User 一样。如果您想检查一个实体“属于”某个用户,那么我想您必须将此信息存储在某个地方。在你的情况下,我不明白为什么你不能使 User-->ContactInfo 关联双向。你能澄清一下吗?

关于java - "cascading" hibernate 实体监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2333156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com