【问题标题】:Deletions with Spring data/Hibernate: ORA-01407: Cannot update to Null使用 Spring 数据/Hibernate 删除:ORA-01407:无法更新为 Null
【发布时间】:2016-03-10 11:33:43
【问题描述】:

我正在尝试使用CRUD Repository 中的deleteAll() 方法从我的数据库中删除某个表中的行

当我这样做时,我收到与我的 DOG 表有关的错误,行 DOG_OWNER:

ORA-01407: Cannot update DOG_OWNER to Null

修复只是删除此行上的not-null constraint,还是有其他解决方法?

【问题讨论】:

  • 添加相关实体和执行deleteAll操作的代码
  • 如果DOG_OWNER 不可为空,您也应该删除引用的DOG 条目
  • DOG_OWNER 是行,我正在尝试删除 DOG 表中的行
  • 如果您在日志中设置了休眠的 sql 打印设置,能否使用日志中的 sql 更新您的问题?

标签: java spring oracle hibernate


【解决方案1】:

下面是一个简化的场景,其中FOREIGN KEYnot NULLable 和使用ON DELETE SET NULL 的约束,这会导致在删除引用的键后报告错误。

在这种情况下确实有助于解除外键上的NOT NULLconstraint。

create table dog_owner 
(id number);
alter table dog_owner add  primary key (id);
insert into dog_owner values (1);

create table dog 
(id number,
dog_owner_id number not null); -- foreign key is not nullable ..
alter table dog add  primary key (id);
alter table dog add  foreign key (dog_owner_id) references dog_owner(id) 
ON DELETE SET NULL; -- but the contrains sets is to null...

insert into dog values (1,1);
commit;

delete from dog_owner where id = 1;

-- SQL-Fehler: ORA-01407: cannot update ("SCHEMA_NAME"."DOG"."DOG_OWNER_ID") to NULL 

alter table dog modify (dog_owner_id number  null);

delete from dog_owner where id = 1;

-- 1 rows deleted

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2023-01-19
    • 2011-05-04
    • 2019-12-31
    • 1970-01-01
    • 2011-08-23
    • 2012-09-11
    相关资源
    最近更新 更多