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

前端之家收集整理的这篇文章主要介绍了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);
       }
    }
}

内部职业实体@H_404_5@

@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里面我有这个@H_404_5@

@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不起作用.不知道为什么?请帮忙.我需要驱逐缓存吗?@H_404_5@

解决方法

如果职业只是这种关系的一部分,那么你可以保证当一个职业从用户的集合中删除时,它也将通过在关系的OneToMany一侧打开orphanRemoval从数据库删除.
@OneToMany(mappedBy="user",orphanRemoval=true)
private List<Profession> professions;

这就是JPA 2.0规范所述的内容@H_404_5@

JPA 2.0规范指出了这一点@H_404_5@

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

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

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].@H_404_5@

猜你在找的Java相关文章