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

如何删除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].

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


应用场景 C端用户提交工单、工单创建完成之后、会发布一条工单创建完成的消息事件(异步消息)、MQ消费者收到消息之后、会通知各处理器处理该消息、各处理器处理完后都会发布一条将该工单写入搜索引擎的消息、最终该工单出现在搜索引擎、被工单处理人检索和处理。 事故异常体现 1、异常体现 从工单的流转记录发现、
线程类,设置有一个公共资源 package cn.org.chris.concurrent; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Descrip
Java中的数字(带有0前缀和字符串)
在Java 9中使用JLink的目的是什么?
Java Stream API Filter(过滤器)
在Java中找到正数和负数数组元素的数量
Java 9中JShell中的不同启动脚本是什么?
使用Java的位填充错误检测技术
java中string是什么
如何使用Java中的JSON-lib API将Map转换为JSON对象?
Java菜单驱动程序以检查数字类型
使用Junit的Maven项目 - 检查银行账号
JAVA编程基础
在Java中使用throw、catch和instanceof来处理异常
在Java中,将数组分割为基于给定查询的子数组后,找到子数组的最大子数组和
如何在Java中从给定的字符串中删除HTML标签?
在PHP中,IntlChar getBlockCode()函数的翻译如下:
如何在Android中实现按下返回键再次退出的功能?
如何使用Java中的流式API解析JSON字符串?
Java中的模式类