我可以使嵌入式Hibernate实体不可为空吗? [英] Can I make an embedded Hibernate entity non-nullable?

查看:93
本文介绍了我可以使嵌入式Hibernate实体不可为空吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的:

  @Embedded(nullable = false)
private Direito direito;

然而,正如您所知,@Embeddable没有这样的属性。



有没有正确的方法来做到这一点?我不想要解决方法。

解决方案

可嵌入组件(或复合元素,无论你想调用它们)通常包含多于一个属性,因此被映射到多个列。因此可以用不同的方式处理整个零部件; J2EE规范没有规定这种或那种方式。



如果Hibernate的所有属性都为NULL(反之亦然),则认为它是NULL。因此,您可以声明一个(任何)属性不为null(在 @Embeddable 中或作为 @AttributeOverride 可以使用 @NotNull 来注释您的属性,但这只会导致应用程序级别的检查,而不会导致数据库级别。


What I want:

@Embedded(nullable = false)
private Direito direito;

However, as you know there's no such attribute to @Embeddable.

Is there a correct way to do this? I don't want workarounds.

解决方案

Embeddable components (or composite elements, whatever you want to call them) usually contain more than one property and thus are mapped to more than one column. The entire component being null can therefore be treated in different ways; J2EE spec does not dictate one way or another.

Hibernate considers component to be NULL if all its properties are NULL (and vice versa). You can therefore declare one (any) of the properties to be not null (either within @Embeddable or as part of @AttributeOverride on @Embedded) to achieve what you want.

Alternatively, if you're using Hibernate Validator you can annotate your property with @NotNull although this will only result in app-level check, not db-level.

这篇关于我可以使嵌入式Hibernate实体不可为空吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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