我有两个实体.使用Post-> Thread的 OnetoOne映射的线程实体和Post实体.
一个线程实体包含许多文章.我知道我应该使用OnetoMany而不是OnetoOne,但为了避免所有收集问题我正在使用OnetoOne
现在的问题是,当我删除一个线程时,还必须删除与之关联的所有帖子.我成功地使用了它
@OnDelete(action = OnDeleteAction.CASCADE)
Run Code Online (Sandbox Code Playgroud)
但它只适用于Postgres和Ms-SQl,但不适用于MySql(也尝试过InnoDb).在上删除级联未在架构生成查询生成.
以下是代码
//Thread Entity
@Id
@GeneratedValue
@Column(name = "thread_id")
private int ThreadID;
Run Code Online (Sandbox Code Playgroud)
//Post Entity
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "post_id")
private int PostID;
@OneToOne()
@OnDelete(action = OnDeleteAction.CASCADE)
private thread ThreadID;
Run Code Online (Sandbox Code Playgroud)
我使用以下查询从Thread实体中删除项目时出现以下错误
session.delete(session.load(thread.class,1));
Run Code Online (Sandbox Code Playgroud)
2014年9月2日下午8:33:51 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN:SQL错误:1451,SQLState:23000 Sep 02,2014 8:33:51 PM org.hibernate.engine.jdbc. spi.SqlExceptionHelper logExceptions错误:无法删除或更新父行:外键约束失败(
forum.post_tbl,CONSTRAINTFK_bfbv5nknqj7ppd5630scimhtbFOREIGN KEY(ThreadID_thread_id)REFERENCESthread_tbl …
我正在尝试使用带有弹簧靴的H2嵌入式DB.但是,它会引发以下错误.
2016-10-19 22:05:25.818 INFO 14104 --- [ main] com.example.SpringDemoApplication : Starting SpringDemoApplication on BOM1-LPMP12AS7U with PID 14104 (D:\workspace\eclipse\SpringDemo\target\classes started by e3028311 in D:\workspace\eclipse\SpringDemo)
2016-10-19 22:05:25.822 INFO 14104 --- [ main] com.example.SpringDemoApplication : No active profile set, falling back to default profiles: default
2016-10-19 22:05:25.904 INFO 14104 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@543e710e: startup date [Wed Oct 19 22:05:25 IST 2016]; root of context hierarchy
2016-10-19 22:05:27.609 INFO 14104 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$ece75ca4] …Run Code Online (Sandbox Code Playgroud)