|
I'm having 2 Entities. *Thread* entity and *Post* entity using OnetoOne mapping from *Post->Thread.*
Now the problem is, when i delete a *Thread, all the **post* associated with it must also be removed. I'm successful in doing it by using
@OnDelete(action = OnDeleteAction.CASCADE)
But it works only on Postgres and Ms-SQl but not on MySql(Tried InnoDb as well). The *on delete cascade* is not generated in the schema generation query.
Following are the code
@Id
@GeneratedValue
@Column(name = "thread_id")
private int ThreadID;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "post_id")
private int PostID;
@OneToOne()
@OnDelete(action = OnDeleteAction.CASCADE)
private thread ThreadID;
I get the following error While deleting an item from *Thread* entity using the following query
session.delete(session.load(thread.class,1));
ERROR: Cannot delete or update a parent row: a foreign key constraint fails (`forum`.`post_tbl`, CONSTRAINT `FK_bfbv5nknqj7ppd5630scimhtb` FOREIGN KEY (`ThreadID_thread_id`) REFERENCES `thread_tbl` (`thread_id`)) org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`forum`.`post_tbl`, CONSTRAINT `FK_bfbv5nknqj7ppd5630scimhtb` FOREIGN KEY (`ThreadID_thread_id`) REFERENCES `thread_tbl` (`thread_id`))
|