@OneToOne(可选= false)和@JoinColumn(nullable = false)一起使用 [英] @OneToOne(optional=false) and @JoinColumn(nullable=false) used together

查看:1809
本文介绍了@OneToOne(可选= false)和@JoinColumn(nullable = false)一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JPA 2.0 FR规范11.1.37中遇到了这个例子。 OneToOne注释,第403页:

I've bumped into this example in JPA 2.0 FR Specification, 11.1.37. OneToOne Annotation, page 403:

@OneToOne(optional=false)
@JoinColumn(name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
public CustomerRecord getCustomerRecord() { return customerRecord; }

我有理由把 @OneToOne(可选= false) )并在同一时间放入 @JoinColumn(... nullable = false)

Is there any reason that I should put @OneToOne(optional=false) and at that same time put @JoinColumn(... nullable=false)?

这两个声明不一样吗?是不是其中一个是多余的?

它们是否都用于DDL模式生成?

Aren't these two declarations the same? Isn't one of them redundant?
Are both of them used in DDL schema generation?

推荐答案

正式 optional = false 是JPA实现的运行时指令, nullable = false 是DDL生成器的指令。所以它们并不是严格多余的。

Formally optional=false is a runtime instruction to the JPA implementation, and nullable=false is an instruction to the DDL generator. So they are not strictly redundant.

当涉及实体继承时,差异可能会变得很大。如果特定映射仅存在于子类上,并且您具有单个表表每层次结构策略,那么OneToOne映射可能是 optional = false 在包含该子类的特定子类上映射。但是,实际的连接列不能为空 - 因为那时共享表的其他子类不能插入!

The difference can become significant when there is entity inheritance involved. If a particular mapping exists only on a subclass, and you have single table table per-hierarchy strategy, then the OneToOne mapping may be optional=false on the particular subclass that contains the mapping. However, the actual join column cannot be made not-null, since then other sub classes that share the table can't be inserted!

实际上不同版本的不同提供者可能会或可能不会在任何时候解释任何一个,告诫经纪人。

In practice different versions of different providers may or may not interpret either one at either time, caveat emptor.

这篇关于@OneToOne(可选= false)和@JoinColumn(nullable = false)一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆