java – Hibernate OnDelete Cascade不适用于MySql,但适用于postgres和Ms-Sql

前端之家收集整理的这篇文章主要介绍了java – Hibernate OnDelete Cascade不适用于MySql,但适用于postgres和Ms-Sql前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有两个实体.使用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;

我使用以下查询从Thread实体中删除项目时出现以下错误

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,CONSTRAINT FK_bfbv5nknqj7ppd5630scimhtb FOREIGN KEY (ThreadID_thread_id) REFERENCES thread_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,CONSTRAINT FK_bfbv5nknqj7ppd5630scimhtb FOREIGN KEY (ThreadID_thread_id) REFERENCES thread_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)

请帮我找到解决方法.

最佳答案
这就是我基于Spring Boot解决这个问题的方法.

// 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;
    }
原文链接:https://www.f2er.com/mysql/433016.html

猜你在找的MySQL相关文章