如果该行的列是外键并且其值为null,我该如何向表中添加一行。
如何使用JPA执行此操作。
public class ExtractionConfig {
private RunHistory runHistoryByLastRunId;
@OneToOne
@JoinColumn(name = "last_run_id", referencedColumnName = "run_id", insertable = false, updatable = false)
public RunHistory getRunHistoryByLastRunId() {
return runHistoryByLastRunId;
}
}
这就是我保存它的方式
ExtractionConfig extractionConfig = new ExtractionConfig();
extractionConfigRepository.save(extractionConfig);
数据库表sql
CREATE TABLE IF NOT EXISTS `camel`.`extraction_config` (
`last_run_id` INT(11) NULL DEFAULT NULL,
INDEX `fk_extraction_config_1_idx` (`last_run_id` ASC),
CONSTRAINT `fk_extraction_config_1`
FOREIGN KEY (`last_run_id`)
REFERENCES `camel`.`run_history` (`run_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
我得到的例外
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2884)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3384)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:451)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:336)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:335)
at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:50)
at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1193)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1261)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:567)
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:482)
... 56 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`camel`.`extraction_config`, CONSTRAINT `fk_extraction_config_1` FOREIGN KEY (`last_run_id`) REFERENCES `run_history` (`run_id`) ON UPDATE CASCADE)
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:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 69 more
我想将ExtractionConfig
对象保存到没有/ null RunHistory
引用的数据库。我怎样才能做到这一点。
答案 0 :(得分:0)
@OneToOne
注释,这告诉hibernate run_id
表中存在camel
。您必须放弃约束。
答案 1 :(得分:0)
我通过将字段设置为null
来解决这个问题。
ExtractionConfig extractionConfig = new ExtractionConfig();
extractionConfig.setRrunHistoryByLastRunId(null);
extractionConfigRepository.save(extractionConfig);