Deleting JPA entity containing @CollectionOfElements throws ConstraintViolationException
- by Lyle
I'm trying to delete entities which contain lists of Integer, and I'm getting ConstraintViolationExceptions because of the foreign key on the table generated to hold the integers. It appears that the delete isn't cascading to the mapped collection.
I've done quite a bit of searching, but all of the examples I've seen on how to accomplish this are in reference to a mapped collection of other entities which can be annotated; here I'm just storing a list of Integer. Here is the relevant excerpt from the class I'm storing:
@Entity
@Table(name="CHANGE_IDS")
@GenericGenerator(
name = "CHANGE_ID_GEN",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
@Parameter(name="sequence_name", value="course_changes_seq"),
@Parameter(name="increment_size", value="5000"),
@Parameter(name=" optimizer", value="pooled")
}
)
@NamedQueries ({
@NamedQuery(
name="Changes.getByStatus",
query= "SELECT c " +
"FROM DChanges c " +
"WHERE c.status = :status "),
@NamedQuery(
name="Changes.deleteByStatus",
query= "DELETE " +
"FROM Changes c " +
"WHERE c.status = :status ")
})
public class Changes {
@Id
@GeneratedValue(generator="CHANGE_ID_GEN")
@Column(name = "ID")
private final long id;
@Enumerated(EnumType.STRING)
@Column(name = "STATUS", length = 20, nullable = false)
private final Status status;
@Column(name="DOC_ID")
@org.hibernate.annotations.CollectionOfElements
@org.hibernate.annotations.IndexColumn(name="DOC_ID_ORDER")
private List<Integer> docIds;
}
I'm deleting the Changes objects using a @NamedQuery:
final Query deleteQuery = this.entityManager.createNamedQuery("Changes.deleteByStatus");
deleteQuery.setParameter("status", Status.POST_FLIP);
final int deleted = deleteQuery.executeUpdate();
this.logger.info("Deleted " + deleted + " POST_FLIP Changes");