加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:02:22 所属栏目:Java 来源:网络整理
导读:如何删除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
如何删除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].

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读