Good morning all,
I have a @ManyToMany relationship and I want to delete all rows from the table. (for automated testing purposes)
The remove process works when using the following code:
Code:
Setor setor = (Setor) entityManager.getReference(Setor.class, new Integer(1883));
entityManager.remove(setor);
but not when executing a delete query like:
Code:
entityManager.createQuery("delete from Setor").executeUpdate();
it fails when deleting the entity because the jointable has a fk restriction linked to the Setor primary key.
Code:
org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FKAF61067472AD19E2: PUBLIC.SETOR_USUARIO FOREIGN KEY(SETORES_CDSETOR) REFERENCES PUBLIC.SETOR(CDSETOR)"
It really feels cumbersome to select a entity before remove it. is that a JPA limitation? or is there something wrong with my mapping?
My mapping:
Code:
@Entity
public class Setor {
@Id
@Column(name = "cdSetor")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer codigo;
@ManyToMany
private Collection<Usuario> usuarios = new LinkedHashSet<Usuario>();
Code:
public class Usuario {
@Id
@Column(name = "cdUsuario")
private String codigo;
@ManyToMany(mappedBy = "usuarios")
private Collection<Setor> setores = new LinkedHashSet<Setor>();