@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(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 举报,一经查实,本站将立刻删除。