How to delete an object in a OneToMany relationship.
@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); } } }
Domestic profession
@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; }
Then inside my EJB I have this
@Stateless @LocalBean @TransactionAttribute(TransactionAttributeType.REQUIRED) public class ScholarEJB{ public void addProfession(User user, Profession profession){
Now addProfession
works beautifully, but removeProfession
does not work. Do not know why? Please help me. Do I need to evict caches?
source share