yet another many-to-many Hibernate questions. I have the simplest possible many-to-many mapping as follows:
@Entity
public class Strategy implements Serializable {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "STRATEGY_TO_GROUP", joinColumns = {@JoinColumn(name="STRATEGY_ID")}, inverseJoinColumns = {@JoinColumn(name = "STRATEGY_GROUP_ID")})
private Set<StrategyGroup> groups;
...
}
And the opposite side of the relation as follows:
@Entity
public class StrategyGroup implements Serializable {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "STRATEGY_TO_GROUP", joinColumns = {@JoinColumn(name="STRATEGY_GROUP_ID")}, inverseJoinColumns = {@JoinColumn(name = "STRATEGY_ID")})
private Set<Strategy> strategies = new HashSet<Strategy>();
What I want to do now is empty both tables in the easiest possible way. I am trying following (em is my entityManager).
em.createQuery("delete from StrategyGroup sg").executeUpdate();
em.createQuery("delete from Strategy s").executeUpdate();
This gives me constraint violation on the @joinTable. On the other hand if I delete by em.remove(strategyGroup);
ti works fine - the strategy group gets deleted and the @joinTable is updated correctly.
So how do I empty the table? Do I need to load the objects and delete them one by one?
Thanks for help.