简体   繁体   English

如何映射Hibernate集合与Oracle强制执行的NOT NULL列约束?

[英]How do I map Hibernate collections with Oracle's NOT NULL column constraint enforced?

I have 2 tables, ENQUIRY and ELEMENT with a One-to-Many relationship such that an Enquiry has many Elements. 我有2个表,INQUIRY和ELEMENT具有一对多关系,因此查询有许多元素。

I would like to enforce Oracle's NOT NULL constraint on foreign key column ELEMENT.ENQUIRY_ID as this is best-practice. 我想在外键列ELEMENT.ENQUIRY_ID上强制执行Oracle的NOT NULL约束,因为这是最佳实践。 I have the following collection on the Enquiry object: 我在Inquiry对象上有以下集合:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "ENQUIRY_ID", referencedColumnName = "ID")
private Set<Element> elements = new HashSet<Element>();

When I enforce the NOT NULL constraint I receive the following stacktrace: 当我强制执行NOT NULL约束时,我收到以下stacktrace:

Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("ELEMENT"."ENQUIRY_ID") 引起:java.sql.BatchUpdateException:ORA-01400:无法插入NULL(“ELEMENT”。“ENQUIRY_ID”)

So Hibernate is obviously persisting the collection of elements before the parent enquiry and then going back and doing an UPDATE on the foreign key field afterwards. 因此,Hibernate显然是在父查询之前持久收集元素,然后返回并在之后对外键字段进行UPDATE。

Is there a way to enforce the NOT NULL constraint on the collection foreign key field? 有没有办法在集合外键字段上强制执行NOT NULL约束?

你在@JoinColumn尝试过nullable = false吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM