I have two related clases JPA-annotated. Alarm and Status. One Alarm can have one Status.
What I need is to be able to delete one Status and "propagate" a null value to the Alarms that are in that Status that has been deleted.
That is, I need the foreign key to be defined as "on delete set null".
@Entity
public class Alarm {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence")
@SequenceGenerator(name="sequence", sequenceName="alarm_pk_seq")
private Integer id;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="idStatus")
private Status status;
// get/set
}
@Entity
public class Status {
@Id
@Column(name="idStatus")
private Integer id;
private String description;
// get/set
}
Example:
Before:
STATUS
id description
1 new
2 assigned
3 closed
ALARMS
id status
1 1
2 2
3 2
After (deleting the status with id=2)
STATUS
id description
1 new
3 closed
ALARMS
id status
1 1
2 NULL
3 NULL
I am using Hibernate and PostgreSQL, automatically generating the database from source code. I have tried with every possible CascadeType with no success.
Is there anything wrong in the code ? Is it possible to do it with JPA ?