I have the following entities:
@Entity( access = AccessType.FIELD )
@Table( name = "TB_Keylist" )
class DBKeylist implements Keylist, Serializable
{
@Basic
@Column( name = "KL_UID" )
@Id( generate = GeneratorType.IDENTITY )
private int uid;
...
@OneToMany( targetEntity = DBUnitRecord.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "keylistUID" )
@MapKey
@OrderBy( clause="UR_UID ASC" )
@OnDelete( action = OnDeleteAction.CASCADE )
private Map< Long, UnitRecord > unitRecords;
}
@Entity( access = AccessType.FIELD )
@Table( name = "TB_UnitRecord" )
class DBUnitRecord implements UnitRecord, Serializable
{
@Basic
@Column( name = "UR_UID" )
@Id
private long unitID;
...
@Basic
@Column( name = "KL_UID", nullable = false )
private int keylistUID;
}
For the DBKeylist.unitRecords field, I am trying to get Hibernate to generate an "on delete cascade" to the foreign key constraint. I have to include the @OnDelete annotation to get it to work right. Shouldn't it work correctly when I included the cascade = CascadeType.ALL to the OneToMany annotation? Is this a feature that just hasn't been added to Hibernate yet? Or is the way that you intend it to always work?
(Note that I played the trick of making the keylistUID to be an int in DBUnitRecord to get the effect of unidirectional one-to-many association. I still have the same cascade delete problem if use a bi-directional association where DBUnitRecord.keylistUID would be a DBKeylist instead of an int.)
|