I have been trying to get one-shot delete working on my one-to-many association using only collection semantics but with no luck. Would be great if some one can provide pointers on this.
Code:
// The entities
class Users{
...
public void setPhones(Set<Phone> phones){
this.phones = phones;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="users", orphanRemoval=true)
public Set<Phone> getPhones(){
return this.phones;
}
...
}
class Phone{
...
@ManyToOne()
@JoinColumn(name="USER_ID")
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
...
}
//The application code...
Users u = (Users)s.get(Users.class, new Integer(220));
u.getPhones().clear();
The above results in multiple delete statements to delete the phone entries. I'd rather prefer a single delete statement for the obvious advantage. I am able to perform this in one shot using queries (native or HQL) but I would prefer doing using collection methods due to the number of places I'd have to make this change.
The
manual mentions that one-shot delete can be forced "at any time by discarding (i.e. dereferencing) the original collection and returning a newly instantiated collection with all the current elements.". What does this mean? Replacing the collection with a newly instantiated collection results in an exception about orphan-removal.
The manual also mentions that "One-shot-delete does not apply to collections mapped inverse=true.". Why is that the case? Using version 3.5
Thanks for your inputs.
PS: Have asked this question
elsewhere, but figure folks here might be able to provide some ideas.