Hibernate cascade remove doesn't remove child when deleting parent. causing this error:
ORA-02292: violated integrity constraint (owner.constraintname)- child record found.
Here are my entities:
@Entity
@Table(name = "FLIGHT_MAP_REGION")
@Audited
public class FlightMapRegion extends AbstractBWSModel {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "FLIGHT_MAP_REGION_ID_GENERATOR", sequenceName = "SEQ_FLIGHT_MAP_REGION", allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FLIGHT_MAP_REGION_ID_GENERATOR")
private Long ID;
@Column(name = "CODE")
private String code;
// bi-directional one-to-many association to FlightMapRegionI18n
@OneToMany (mappedBy = "flightMapRegion", fetch = FetchType.EAGER, cascade=CascadeType.REMOVE, orphanRemoval=true)
@Fetch(value = FetchMode.SUBSELECT)
@NotAudited
private List<FlightMapRegionI18n> flightMapRegionI18ns;
//getters&setters
and:
@Entity
@Table(name = "FLIGHT_MAP_REGION_I18N")
@Audited
public class FlightMapRegionI18n extends AbstractBWSModel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "FLIGHT_MAP_REGION_I18N_ID_GENERATOR", sequenceName = "SEQ_FLIGHT_MAP_REGION_I18N", allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FLIGHT_MAP_REGION_I18N_ID_GENERATOR")
private Long ID;
@Column(name = "NAME")
private String name;
// bi-directional many-to-one association to flightMapRegion
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FLIGHT_MAP_REGION_CODE", referencedColumnName="CODE")
@AuditJoinTable
private FlightMapRegion flightMapRegion;
//getters & setters
I want to delete any child FlightMapRegionI18n records when deleting the parent FlightMapRegion record.
Note that these tables are not connected via id, they are connected like this:
FOREIGN KEY (FLIGHT_MAP_REGION_CODE)
REFERENCES FLIGHT_MAP_REGION (CODE)
And I don't want to add ON DELETE CASCADE
on database level. I want to achive this programmatically in java.