Java @Basic 注释到底有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23575884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:05:19  来源:igfitidea点击:

What does @Basic annotation really do?

javahibernatejpa

提问by Erran Morad

It seems that @Basicannotation on a java variable only declares that the variable must be saved as a column with NOT NULL constraint. Is that correct ?

似乎@Basicjava 变量上的注释仅声明该变量必须保存为具有 NOT NULL 约束的列。那是对的吗 ?

This postsays that:

这个帖子说:

@Basic(optional = false) @Column(nullable = false) The @Basic annotation marks the property as not optional on the Java object level. The second setting, nullable = false on the column mapping, is only responsible for the generation of a NOT NULL database constraint. The Hibernate JPA implementation treats both options the same way in any case, so you may as well use only one of the annotations for this purpose.

@Basic(optional = false) @Column(nullable = false) @Basic 注释在Java 对象级别将属性标记为非可选。列映射上的第二个设置 nullable = false 仅负责生成 NOT NULL 数据库约束。Hibernate JPA 实现在任何情况下都以相同的方式处理这两个选项,因此您最好为此目的仅使用其中一个注释。

I am confused. What does this mean - The @Basic annotation marks the property as not optional on the Java object level.How is a property or variable "optional" at Java level ?

我很迷惑。这是什么意思 - The @Basic annotation marks the property as not optional on the Java object level.Java 级别的属性或变量如何“可选”?

采纳答案by Alan Hay

The Hibernate JPA implementation will treat both the same only in terms of schema generation, that is the column will be created with a not null constraint.

Hibernate JPA 实现将仅在模式生成方面对待两者相同,即列将使用非空约束创建。

Using optional = false however also allows for Hibernate (and I suppose other implementations) to perform a check and throw an exception prior to flushing to the database if the non-optional field is null. Without this you would only get an Exception thrown after the attempt to insert.

但是,如果非可选字段为空,则使用 optional = false 还允许 Hibernate(以及我假设其他实现)在刷新到数据库之前执行检查并抛出异常。如果没有这个,您只会在尝试插入后抛出异常。

From Pro JPA:

来自 Pro JPA:

When the optional element is specified as false, it indicates to the provider that the field or property mapping may not be null. The API does not actually define what the behavior is in the case when the value is null, but the provider may choose to throw an exception or simply do something else. For basic mappings, it is only a hint and can be completely ignored. The optional element may also be used by the provider when doing schema generation, because, if optional is set to true, then the column in the database must also be nullable.

当可选元素被指定为 false 时,它​​向提供者表明该字段或属性映射不能为空。API 并没有真正定义在值为 null 的情况下的行为,但提供者可能会选择抛出异常或简单地做其他事情。对于基本映射,它只是一个提示,可以完全忽略。在进行模式生成时,提供者也可以使用 optional 元素,因为如果 optional 设置为 true,那么数据库中的列也必须可以为空。

Having optional=false can also affect Entity loading in Hibernate. For example, single-ended associations are always eagerly loaded in Hibernate unless the association is marked as optional=false.

optional=false 也会影响 Hibernate 中的实体加载。例如,单端关联总是在 Hibernate 中急切加载,除非关联被标记为 optional=false。

See: https://stackoverflow.com/a/17987718/1356423for further explanation.

请参阅:https: //stackoverflow.com/a/17987718/1356423以获取进一步说明。

回答by meriton

The authoritative answer to the meaning of an api element is of course the api documentation, i.e. the javadoc. For the @Basic annotation, this writes:

api元素含义的权威答案当然是api文档,即javadoc。对于@Basic 注释,它写道

The simplest type of mapping to a database column. The Basic annotation can be applied to a persistent property or instance variable of any of the following types: Java primitive types, wrappers of the primitive types, String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], enums, and any other type that implements java.io.Serializable.

The use of the Basic annotation is optional for persistent fields and properties of these types. If the Basic annotation is not specified for such a field or property, the default values of the Basic annotation will apply.

到数据库列的最简单映射类型。Basic 注释可以应用于以下任何类型的持久属性或实例变量:Java 原始类型、原始类型的包装器、String、java.math.BigInteger、java.math.BigDecimal、java.util.Date、 java.util.Calendar、java.sql.Date、java.sql.Time、java.sql.Timestamp、byte[]、Byte[]、char[]、Character[]、枚举和任何其他实现 java 的类型。 io.可序列化。

对于这些类型的持久字段和属性,基本注释的使用是可选的。如果未为此类字段或属性指定 Basic 注释,则将应用 Basic 注释的默认值。

What are the values of the Basic annotation? The Javadoc explains them, too:

Basic 注释的值是什么?Javadoc 也解释了它们:

public abstract FetchType fetch

(Optional) Defines whether the value of the field or property should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistence provider runtime that the value must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime. If not specified, defaults to EAGER.

public abstract FetchType fetch

(可选)定义字段或属性的值是应该延迟加载还是必须急切地获取。EAGER 策略是对持久性提供程序运行时的一项要求,即必须急切地获取值。LAZY 策略是对持久性提供程序运行时的提示。如果未指定,则默认为 EAGER。

and

public abstract boolean optional

(Optional) Defines whether the value of the field or property may be null. This is a hint and is disregarded for primitive types; it may be used in schema generation. If not specified, defaults to true.

public abstract boolean optional

(可选)定义字段或属性的值是否可以为空。这是一个提示,对于原始类型不予考虑;它可以用于模式生成。如果未指定,则默认为 true。

Therefore, if you set optionalto false, the persistence provider may throw an exception when you try to persist or update an object where the property is null. This can be useful if your business rules say that null is not a legal value.

因此,如果设置optionalfalse,则当您尝试持久化或更新属性为 null 的对象时,持久性提供程序可能会抛出异常。如果您的业务规则表明 null 不是合法值,这会很有用。

Note

笔记

At least when using hibernate, nullability is better expressed with the corresponding Bean Validation annotation (@NotNull), as this annotation is both understood by hibernate and can be used by other layers on an application (for instance when validating user input).

至少在使用 hibernate 时,可以使用相应的 Bean Validation 注释 ( @NotNull)更好地表达可空性,因为该注释既可以被 hibernate 理解,也可以被应用程序上的其他层使用(例如,在验证用户输入时)。