【问题标题】:Spring JPA Crud Repository Save Not Returning UUID FieldSpring JPA Crud 存储库保存不返回 UUID 字段
【发布时间】:2018-10-02 16:06:07
【问题描述】:

我正在尝试使用纯 JPA 保存实体,但保存时未返回 Postgres UUID 字段。 UUID 字段默认使用public.uuid_generate_v1mc() 自动生成。我使用 postgres 作为数据库,由于某种原因,在 jpa 中进行保存时创建的默认值不会返回。我尝试添加一个@Generated 字段并将其设置为自动,但这没有任何作用。

我做错了吗?

数据库创建语句

CREATE TABLE usermgmt.test
(
  id SERIAL PRIMARY KEY,
  active boolean NOT NULL DEFAULT false,
  created timestamp without time zone NOT NULL DEFAULT now(),
  updated timestamp without time zone NOT NULL DEFAULT now(),
  description character varying,
  name character varying NOT NULL,
  external_id bigint UNIQUE ,
  uuid uuid DEFAULT public.uuid_generate_v1mc(),
  alias character varying NOT NULL UNIQUE,
  code character varying NOT NULL UNIQUE
);

实体

@SuppressWarnings("serial")
@Entity(name = "managementTest")
@Cacheable
@Table(schema = "usermgmt", name = "test")
public class Test extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String description;

    @Column(nullable = false, insertable = false, updatable = false)
    private UUID uuid;

    @Column(nullable = false, unique = true)
    private String alias;

    @Column(nullable = false, unique = true)
    private String code;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the uuid
     */
    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

基础实体

@MappedSuperclass
public abstract class BaseEntity {
    private LocalDateTime created;
    private LocalDateTime updated;
    @NotNull
    private Boolean active;

    public BaseEntity() {
        this.active = Boolean.TRUE;
    }

    @PrePersist
    @PreUpdate
    void setDates() {
        if (this.created == null) {
            this.created = LocalDateTime.now();
        }

        this.updated = LocalDateTime.now();
    }

    public Boolean getActive() {
        return this.active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public LocalDateTime getCreated() {
        return this.created;
    }

    public LocalDateTime getUpdated() {
        return this.updated;
    }

    public void setUpdated(LocalDateTime updated) {
        this.updated = updated;
    }

    public void touch() {
        this.updated = LocalDateTime.now();
    }
}

粗制滥造

@Repository("managementTestCrudRepository")
public interface TestCrudRepository extends JpaRepository<Test, Long> {
    Test findByCode(String code);
    Test findByUuid(UUID uuid);
    Test findByAlias(String alias);
    List<Test> findByActive(Boolean active);
}

这是我用来保存实体的方法

@PutMapping
public Test putByJson(@RequestBody String json) {
    return testCrudRepository.save(new Gson().fromJson(json, Test.class));
}

邮递员返回的对象

{
    "created": {
        "month": "OCTOBER",
        "year": 2018,
        "dayOfYear": 275,
        "hour": 11,
        "minute": 9,
        "nano": 325000000,
        "second": 52,
        "dayOfMonth": 2,
        "dayOfWeek": "TUESDAY",
        "monthValue": 10,
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601"
        }
    },
    "updated": {
        "month": "OCTOBER",
        "year": 2018,
        "dayOfYear": 275,
        "hour": 11,
        "minute": 9,
        "nano": 329000000,
        "second": 52,
        "dayOfMonth": 2,
        "dayOfWeek": "TUESDAY",
        "monthValue": 10,
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601"
        }
    },
    "active": true,
    "id": 2,
    "externalId": null,
    "name": "test1",
    "description": "test1",
    "uuid": null,
    "alias": "test1",
    "code": "test1"
}

【问题讨论】:

  • 你用的是哪个@Generated?那里有多个,可能很容易混淆它们。 Hibernate 是否还执行了从@Generated 获得的额外选择?

标签: java spring postgresql spring-boot spring-data-jpa


【解决方案1】:

解决方案

您需要在将由数据库生成的字段上添加@Generated 注释。

@Generated(GenerationTime.ALWAYS)
private UUID uuid;

说明

您需要找到一种方法来告诉您的 JPA 提供者 (1) 在数据库端生成的字段以及 (2) 何时获取它们。

对于此类属性,有一个名为@Generated 的注解,它提供了两个选项:

  • 在插入时获取生成的属性 (GenerationTime.INSERT);
  • 在插入和更新时获取生成的属性 (GenerationTime.ALWAYS)。

要检索所需的 @Generated 值,Hibernate(Spring 将其用作默认 JPA 提供程序)将执行额外的 SELECT 语句。

我不知道您将使用PUT 端点做什么(创建/更新/覆盖资源1),但我会使用@Generated(ALWAYS),因为使用@987654335 @,您将在更新/合并时获得null

奖金

我觉得这个视频信息量很大,很中肯。


1a serious discussion here

【讨论】:

  • 感谢您提供详细信息。自从您发布此内容后,我将始终将其更新为!所以感谢您花时间回答:)。
【解决方案2】:

所以我发现你必须使用注解@Generated(GenerationTime.INSERT)

我实际上是在使用@GeneratedValue,我为此尝试了所有不同的值,但没有一个起作用。最后在使用 @Generated(GenerationTime.INSERT) 之后它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2018-11-09
    • 2022-10-13
    • 2015-10-04
    相关资源
    最近更新 更多