开发者

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

开发者 https://www.devze.com 2023-02-02 19:40 出处:网络
I have 2 tables, ENQUIRY and ELEMENT with a One-to-Many relationship such that an Enquiry has many Elements.

I have 2 tables, ENQUIRY and ELEMENT with a One-to-Many relationship such that an Enquiry has many Elements.

I would like to enforce Oracle's NOT NULL constraint on foreign key column ELEMENT.ENQUIRY_ID as this is best-practice. I have the following collection on the Enquiry object:

@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:

Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("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.

Is there a way to enforce the NOT N开发者_开发技巧ULL constraint on the collection foreign key field?


Have you tried nullable = false in @JoinColumn?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号