I have a table defined as this:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name = "EVENT_TYPE")
public abstract class Event implements Serializable {
@Id
@GeneratedValue
private Long id;
...
}
Then I have this inherited tables:
@Entity
@DiscriminatorValue("BOOLEANVALUE")
public class BooleanValueEvent extends Event {
...
}
@Entity
@DiscriminatorValue("EXACTVALUE")
public class ExactValueEvent extends Event {
...
}
Hibernate creates two table in database, but when I try to delete on JPA an inherited object it returns foreign key constraint violation error, because It try to delete first Event
class. How can I delete inherited class first when I delete superClass in order to avoid key constraint violation error?
Thank you.
Using cascade delete , removing a SubTable entity will succeed and it will remove both the subTable and the parentTable rows.
@Entity
@DiscriminatorValue("BOOLEANVALUE")
@OnDelete(action = OnDeleteAction.CASCADE)
public class BooleanValueEvent extends Event {
...
}
@Entity
@DiscriminatorValue("EXACTVALUE")
@OnDelete(action = OnDeleteAction.CASCADE)
public class ExactValueEvent extends Event {
...
}
Finally I solved it with.
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "typeName")
@JsonSubTypes({
@Type(value = BooleanValueEvent.class),
@Type(value = ExactValueEvent.class)
})
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name = "EVENT_TYPE")
public abstract class Event implements Cloneable, Serializable {
@Id
@GeneratedValue
private Long id;
...
}
And
@Entity
@DiscriminatorValue("BOOLEANVALUE")
public class BooleanValueEvent extends Event {
...
}
@Entity
@DiscriminatorValue("EXACTVALUE")
public class ExactValueEvent extends Event {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.