java - 尽管使用了 @Access(value=AccessType.FIELD),但 JPA Hibernate 在 Getter 上使用 @NotNull 注释
问题描述
我有一个使用的实体
@Entity
@Table(
indexes = {@Index(name="URL_PARAMETER_INDEX", unique = true, columnList = "urlParameter")}
)
@Access(value=AccessType.FIELD)
public class EntityA extends UniqueIdEntity {
// From @MappedSuperClass "UniqueIdEntity"
@Id
@GeneratedValue(generator = "uuid2")
@Column(unique = true, updatable = false, columnDefinition = "uuid")
protected UUID id;
@Column(name = "urlParameter", unique = true, nullable = true)
private String urlParameter;
...
@javax.validation.constraints.NotNull
public String getUrlParameter() {
if(this.urlParameter == null) {
this.urlParameter = this.calculateUrlParameter();
}
return urlParameter;
}
}
虽然字段 urlParameter 可以为 null,但在第一次访问时会生成它并返回新生成的值,因此该字段可能为 null,但 getter 永远不会返回 null。
但是,JPA 将其转换为以下 DDL 命令:add column url_parameter varchar(255) not null
当我删除 getter 上的 @NotNull 注释时,它会将列生成为可为空的。
我究竟做错了什么?我已经@Access(value=AccessType.FIELD)
在每个超类上添加了,以确保 JPA/Hibernate 使用正确的访问类型。
如何确保 Column 可以为空,同时在我的 Getter 上仍然有 @NotNull 注释?
解决方案
推荐阅读
- html - 从 CSS 访问 HTML 中的子类时遇到问题
- java - 找到可被 N 整除的最小数字,数字总和为 N
- python - PyTorch 从检查点加载 GradScaler
- javascript - NextJs & Strapi 使用 getStaticPaths 错误
- javascript - 如何获得与承诺相关的返回值
- python - 当 OpenCV 在 venv python 3.10 中时,Pycharm“找不到参考”
- pine-script - 我如何计算会话的总累计牛/熊蜡烛?
- windows - 如何创建热键以在 Windows 中打开文件
- python-3.x - 完全关闭 django rest framework url
- docker - 无法从 macOS M1 本地主机访问容器中运行的 Flask 应用程序