Default Column Values in JPA – JPA中的默认列值

最后修改: 2019年 5月 9日

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

1. Overview

1.概述

In this tutorial, we’ll look into default column values in JPA.

在本教程中,我们将探究JPA中的默认列值。

We’ll learn how to set them as a default property in the entity as well as directly in the SQL table definition.

我们将学习如何将它们设置为实体中的默认属性,以及直接在SQL表定义中设置。

2. While Creating an Entity

2.在创建一个实体的同时

The first way to set a default column value is to set it directly as an entity property value:

设置默认列值的第一个方法是直接将其设置为实体属性值

@Entity
public class User {
    @Id
    private Long id;
    private String firstName = "John Snow";
    private Integer age = 25;
    private Boolean locked = false;
}

Now, every time we create an entity using the new operator, it will set the default values we’ve provided:

现在,每次我们使用new操作符创建一个实体时,它都会设置我们提供的默认值。

@Test
void saveUser_shouldSaveWithDefaultFieldValues() {
    User user = new User();
    user = userRepository.save(user);
    
    assertEquals(user.getName(), "John Snow");
    assertEquals(user.getAge(), 25);
    assertFalse(user.getLocked());
}

There is one drawback to this solution.

这个解决方案有一个缺点。

When we take a look at the SQL table definition, we won’t see any default value in it:

当我们看一下SQL表的定义时,我们不会在其中看到任何默认值。

create table user
(
    id     bigint not null constraint user_pkey primary key,
    name   varchar(255),
    age    integer,
    locked boolean
);

So, if we override them with null, the entity will be saved without any error:

因此,如果我们用null覆盖它们,实体将被保存,不会有任何错误

@Test
void saveUser_shouldSaveWithNullName() {
    User user = new User();
    user.setName(null);
    user.setAge(null);
    user.setLocked(null);
    user = userRepository.save(user);

    assertNull(user.getName());
    assertNull(user.getAge());
    assertNull(user.getLocked());
}

3. In the Schema Definition

3.在模式定义中

To create a default value directly in the SQL table definition, we can use the @Column annotation and set its columnDefinition parameter:

要在SQL表定义中直接创建默认值,我们可以使用@Column注解并设置其columnDefinition参数。

@Entity
public class User {
    @Id
    Long id;

    @Column(columnDefinition = "varchar(255) default 'John Snow'")
    private String name;

    @Column(columnDefinition = "integer default 25")
    private Integer age;

    @Column(columnDefinition = "boolean default false")
    private Boolean locked;
}

Using this method, the default value will be present in the SQL table definition:

使用这种方法,默认值将出现在SQL表定义中。

create table user
(
    id     bigint not null constraint user_pkey primary key,
    name   varchar(255) default 'John Snow',
    age    integer      default 35,
    locked boolean      default false
);

And the entity will be saved properly with the default values:

而实体将以默认值被正确保存。

@Test
void saveUser_shouldSaveWithDefaultSqlValues() {
    User user = new User();
    user = userRepository.save(user);

    assertEquals(user.getName(), "John Snow");
    assertEquals(user.getAge(), 25);
    assertFalse(user.getLocked());
}

Remember that by using this solution, we won’t be able to set a given column to null when saving the entity for the first time. If we don’t provide any value, the default one will be set automatically.

请记住,通过使用这个解决方案,我们将无法在第一次保存实体时将某个列设置为null。如果我们不提供任何值,默认值将被自动设置。

4. Conclusion

4.总结

In this short article, we learned how to set default column values in JPA.

在这篇短文中,我们学习了如何在JPA中设置默认列值。

As always, the full source code is available over on GitHub.

一如既往,完整的源代码可在GitHub上获得