Hi,
I have an entity A that has a collection of basic types (e.g. String). I use such a mapping because the strings associated to each instance of A depend on A's life cycle. If I want to remove an instance of A from the DB, I also want its associated Strings to be removed.
My mapping is as follows:
Code:
@Entity
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "AStrings", joinColumns = @JoinColumn(name = "id"))
@Column(name = "strings", nullable = false)
private Set<String> strings;
}
If I create an instance of A and add it some strings, then I can persist the instance using
Session.save(myInstance). Both the instance of A and its associated Strings are persisted.
But, if I want to remove the same instance from the DB, using
Session.delete(myInstance), I get a foreign key constraint error:
Quote:
Cannot delete or update a parent row: a foreign key constraint fails
But, I would expect the associated Strings to be automatically removed before removing the A's instance, but it seems it's not the case. I also didn't found a way to specify cascade rules.
I've also tried using
@Cascade(CascadeType.DELETE) on the field
strings, and it still doesn't help. By looking at the database, I don't see any
ON DELETE policy for the concerned foreign key.
Is there something wrong with my configuration?
Thanks