gpt4 book ai didi

jpa - JPA2.0 : Delete Entity in OneToMany RelationShip

转载 作者:行者123 更新时间:2023-12-02 09:38:31 24 4
gpt4 key购买 nike

如何删除OneToMany关系中的实体。

@Entity
@NamedQueries({
@NamedQuery(name="User.findByUserNamePassword",
query="select c from User c where c.userName = :userName AND c.password = :password")
})
@Table(name="\"USER\"")
public class User implements Serializable {
@OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemove=true)
private List<Profession> professions;

public List<Profession> getProfessions() {
return professions;
}

public void setProfessions(List<Profession> professions) {
this.professions = professions;
}

public void addProfession(Profession profession){
if(this.professions == null){
this.professions = new ArrayList<Profession>();
}
this.professions.add(profession);
profession.setUser(this);
}

public void removeProfession(Profession profession){
if(this.professions != null){
professions.remove(profession);
profession.setUser(null);
}
}
}

内部职业实体
@Entity
public class Profession implements Serializable {
@ManyToOne
@JoinColumn(name="UserId", nullable=false)
private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

然后在我的EJB中,我有这个
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ScholarEJB{

/**
* Add a profession to a target user
* @param user
* @param profession
*/
public void addProfession(User user, Profession profession){
//Put the user in a managed state. It is important to do this before
//adding a new profession onto user
user = find(User.class, user.getId());
user.addProfession(profession);
this.create(user); //This is persist action
}

public void removeProfession(User user, Profession profession){
//Put the user in a managed state. It is important to do this before
//adding a new profession onto user
user = find(User.class, user.getId());
user.remove(user);
this.update(user); //merge action
//this.create(user) //also try this as well, but it does not work
}
}

现在 addProfession可以很好地工作,但是 removeProfession不起作用。不知道为什么吗?请帮忙。我需要驱逐缓存吗?

最佳答案

如果专业只是该关系的一部分,则可以通过在关系的OneToMany端启用orphanRemoval来保证,当从用户集中删除该专业时,该专业也将从数据库中删除。

@OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemoval=true)
private List<Profession> professions;

这就是JPA 2.0规范指出的内容

JPA 2.0规范指出

Associations that are specified as OneToOne or OneToMany support use of the orphanRemoval option. The following behaviors apply when orphanRemoval is in effect:

If an entity that is the target of the relationship is removed from the relationship (by setting the relationship to null or removing the entity from the relationship collection), the remove operation will be applied to the entity being orphaned. The remove operation is applied at the time of the flush operation. The orphanRemoval functionality is intended for entities that are privately "owned" by their parent entity. Portable applications must otherwise not depend upon a specific order of removal, and must not reassign an entity that has been orphaned to another relationship or otherwise attempt to persist it. If the entity being orphaned is a detached, new,or removed entity, the semantics of orphanRemoval do not apply.

If the remove operation is applied to a managed source entity, the remove operation will be cascaded to the relationship target in accordance with the rules of section 3.2.3, (and hence it is not necessary to specify cascade=REMOVE for the relationship)[20].

关于jpa - JPA2.0 : Delete Entity in OneToMany RelationShip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5642002/

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