JPA2.0:删除OneToMany RelationShip中的实体

@H_404_0@
如何删除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,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",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].

相关文章

ArrayList简介:ArrayList 的底层是数组队列,相当于动态数组。与 Java 中的数组相比,它的容量能动态增...
一、进程与线程 进程:是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位。 线程...
本文为博客园作者所写:&#160;一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 简单的一个类...
#############java面向对象详解#############1、面向对象基本概念2、类与对象3、类和对象的定义格式4、...
一、什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错。在java中,阻止当前方法或作用域...
Collection接口 Collection接口 Collection接口 Collection是最基本的集合接口,一个Collection代表一组...