JPA @Basic Annotation – JPA @Basic 注释

最后修改: 2019年 5月 10日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll explore the JPA @Basic annotation. We’ll also discuss the difference between @Basic and @Column JPA annotations.

在这个快速教程中,我们将探讨JPA的@Basic注解。我们还将讨论@Basic@Column JPA注解的区别。

2. Basic Types

2.基本类型

JPA support various Java data types as persistable fields of an entity, often known as the basic types.

JPA支持各种Java数据类型作为实体的可持续字段,通常被称为基本类型。

A basic type maps directly to a column in the database. These include Java primitives and their wrapper classes, String, java.math.BigInteger and java.math.BigDecimal, various available date-time classes, enums, and any other type that implements java.io.Serializable.

基本类型直接映射到数据库中的一个列。这些类型包括Java基元及其包装类、Stringjava.math.BigIntegerjava.math.BigDecimal、各种可用的日期-时间类、枚举以及任何其他实现java.io.Serializable的类型。

Hibernate, like any other ORM vendor, maintains a registry of basic types and uses it to resolve a column’s specific org.hibernate.type.Type.

Hibernate和其他ORM厂商一样,维护着一个基本类型的注册表,并使用它来解决一个列的具体org.hibernate.type.Type

3. @Basic Annotation

3.@Basic注释

We can use the @Basic annotation to mark a basic type property:

我们可以使用 @Basic注解来标记一个基本类型的属性。

@Entity
public class Course {

    @Basic
    @Id
    private int id;

    @Basic
    private String name;
    ...
}

In other words, the @Basic annotation on a field or a property signifies that it’s a basic type and Hibernate should use the standard mapping for its persistence.

换句话说,字段或属性上的@Basic注解标志着它是一个基本类型,Hibernate应该使用标准映射来持久化它。

Note that it’s an optional annotation. And so, we can rewrite our Course entity as:

注意,这是一个可选的注释。因此,我们可以将我们的Course实体改写为。

@Entity
public class Course {

    @Id
    private int id;

    private String name;
    ...
}

When we don’t specify the @Basic annotation for a basic type attribute, it is implicitly assumed, and the default values of this annotation apply.

当我们没有为基本类型属性指定@Basic注解时,它被隐含地假定,并且该注解的默认值适用。

4. Why Use @Basic Annotation?

4.为什么使用@Basic注释?

The @Basic annotation has two attributes, optional and fetch. Let’s take a closer look at each one.

@Basic注解有两个属性,optionalfetch。让我们仔细看看每个属性。

The optional attribute is a boolean parameter that defines whether the marked field or property allows null. It defaults to true. So, if the field is not a primitive type, the underlying column is assumed to be nullable by default.

optional属性是一个boolean参数,它定义了标记的字段或属性是否允许null它默认为true。因此,如果字段不是原始类型,底层的列就被默认为nullable

The fetch attribute accepts a member of the enumeration Fetch, which specifies whether the marked field or property should be lazily loaded or eagerly fetched. It defaults to FetchType.EAGER, but we can permit lazy loading by setting it to FetchType.LAZY.

fetch属性接受枚举Fetch中的一个成员,该成员指定标记的字段或属性是应该被懒惰地加载还是急切地获取。它的默认值是FetchType.EAGER,但是我们可以通过设置它为FetchType.LAZY来允许懒惰加载。

Lazy loading will only make sense when we have a large Serializable object mapped as a basic type, as in that case, the field access cost can be significant.

只有当我们有一个大型的Serializable对象被映射为基本类型时,懒惰加载才有意义,因为在这种情况下,字段访问成本会很高。

We have a detailed tutorial covering Eager/Lazy loading in Hibernate that takes a deeper dive into the topic.

我们有一个详细的教程,涵盖Hibernate中的Eager/Lazy loading,对该主题进行了深入的研究。

Now, let’s say don’t want to allow nulls for our Course‘s name and want to lazily load that property as well. Then, we’ll define our Course entity as:

现在,让我们假设不想让我们的Coursename允许nulls,并且想懒散地加载该属性。然后,我们将我们的Course实体定义为。

@Entity
public class Course {
    
    @Id
    private int id;
    
    @Basic(optional = false, fetch = FetchType.LAZY)
    private String name;
    ...
}

We should explicitly use the @Basic annotation when willing to deviate from the default values of optional and fetch parameters. We can specify either one or both of these attributes, depending on our needs.

当我们愿意偏离optionalfetch参数的默认值时,应该明确地使用@Basic注解。我们可以根据我们的需要,指定这些属性中的一个或两个。

5. JPA @Basic vs @Column

5.JPA @Basic@Column的比较

Let’s look at the differences between @Basic and @Column annotations:

让我们看看@Basic@Column注释之间的区别。

  • Attributes of the @Basic annotation are applied to JPA entities, whereas the attributes of @Column are applied to the database columns
  • @Basic annotation’s optional attribute defines whether the entity field can be null or not; on the other hand, @Column annotation’s nullable attribute specifies whether the corresponding database column can be null
  • We can use @Basic to indicate that a field should be lazily loaded
  • The @Column annotation allows us to specify the name of the mapped database column

6. Conclusion

6.结语

In this article, we learned when and how to use JPA’s @Basic annotation. We also talked about how it differs from the @Column annotation.

在这篇文章中,我们了解了何时以及如何使用JPA的@Basic注解。我们还谈到了它与@Column注解的不同之处。

As usual, code examples are available over on Github.

像往常一样,代码实例可在Github上获得