我有两个实体.使用Post-> Thread的OnetoOne映射的线程实体和Post实体.
Thread实体包含许多帖子.我知道我应该使用OnetoMany而不是OnetoOne,但为了避免所有收集问题我正在使用OnetoOne
现在的问题是,当我删除一个线程时,还必须删除与之关联的所有帖子.
我成功地使用了它
@OnDelete(action = OnDeleteAction.CASCADE)
但它只适用于Postgres和Ms-sql,但不适用于MysqL(也尝试过InnoDb).在架构生成查询中不生成on delete cascade.
以下是代码
//Thread Entity
@Id
@GeneratedValue
@Column(name = "thread_id")
private int ThreadID;
//Post Entity
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "post_id")
private int PostID;
@OneToOne()
@OnDelete(action = OnDeleteAction.CASCADE)
private thread ThreadID;
session.delete(session.load(thread.class,1));
Sep 02,2014 8:33:51 PM org.hibernate.engine.jdbc.spi.sqlExceptionHelper logExceptions
WARN: sql Error: 1451,sqlState: 23000
Sep 02,2014 8:33:51 PM org.hibernate.engine.jdbc.spi.sqlExceptionHelper logExceptions
ERROR: Cannot delete or update a parent row: a foreign key constraint fails (forum
.post_tbl
,CONSTRAINTFK_bfbv5nknqj7ppd5630scimhtb
FOREIGN KEY (ThreadID_thread_id
) REFERENCESthread_tbl
(thread_id
))
org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.sqlExceptionTypeDelegate.convert(sqlExceptionTypeDelegate.java:74)
at org.hibernate.exception.internal.StandardsqlExceptionConverter.convert(StandardsqlExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.sqlExceptionHelper.convert(sqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.sqlExceptionHelper.convert(sqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3343)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3546)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:100)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:293)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at test.main(test.java:84)
Caused by: com.MysqL.jdbc.exceptions.jdbc4.MysqLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (forum
.post_tbl
,CONSTRAINTFK_bfbv5nknqj7ppd5630scimhtb
FOREIGN KEY (ThreadID_thread_id
) REFERENCESthread_tbl
(thread_id
))
HibernateException : could not execute statement
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.MysqL.jdbc.Util.handleNewInstance(Util.java:411)
at com.MysqL.jdbc.Util.getInstance(Util.java:386)
at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:1040)
at com.MysqL.jdbc.MysqLIO.checkErrorPacket(MysqLIO.java:4120)
at com.MysqL.jdbc.MysqLIO.checkErrorPacket(MysqLIO.java:4052)
at com.MysqL.jdbc.MysqLIO.sendCommand(MysqLIO.java:2503)
at com.MysqL.jdbc.MysqLIO.sqlQueryDirect(MysqLIO.java:2664)
at com.MysqL.jdbc.ConnectionImpl.execsql(ConnectionImpl.java:2794)
at com.MysqL.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.MysqL.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.MysqL.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.MysqL.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
… 14 more
BUILD SUCCESSFUL (total time: 2 seconds)
请帮我找到解决方法.
解
从
// application.properties
spring.jpa.database-platform=org.hibernate.dialect.MysqLDialect
至
spring.jpa.database-platform=org.hibernate.dialect.MysqL5InnoDBDialect
Explnation
如下所示,似乎MysqL5InnoDBDialect.java中CascadeDelete的默认值仅支持Cascade.
// MysqLDialect.java
@Override
public boolean supportsCascadeDelete() {
return false;
}
// MysqL5InnoDBDialect.java
@Override
public boolean supportsCascadeDelete() {
return true;
}