java Spring JPA会覆写DDL预设值

kwvwclae  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(140)

我定义了一个表,如下所示:

CREATE TABLE CONFIGSERVER.PROPERTIES (
    ID NUMBER GENERATED ALWAYS AS IDENTITY ( START WITH 1 CACHE 20 ) ,
    APPLICATION VARCHAR2 (4000) ,
    PROFILE     VARCHAR2 (4000) ,
    LABEL       VARCHAR2 (4000) ,
    PROP_KEY    VARCHAR2 (4000) NOT NULL,
    VALUE       VARCHAR2 (4000) NOT NULL,
    CREATED_ON  TIMESTAMP DEFAULT SYSDATE,
    CREATED_BY  VARCHAR2 (100) DEFAULT COALESCE(
        REGEXP_SUBSTR(SYS_CONTEXT('USERENV','CLIENT_IDENTIFIER'),'^[^:]*'),
        SYS_CONTEXT('USERENV','SESSION_USER')),
    UPDATED_ON  TIMESTAMP ,
    UPDATED_BY  VARCHAR2 (100)
) LOGGING;

如果使用SQLCl执行以下语句:

insert into properties (application, profile, label, prop_key, value) values ('nisse', 'bosse', 'bengt', 'olle', 'korv med mos');

DDL定义会正确填入CREATED_ONCREATED_BY的值:

SQL> select * from properties where application = 'nisse';

   ID APPLICATION    PROFILE    LABEL    PROP_KEY    VALUE           CREATED_ON                         CREATED_BY      UPDATED_ON    UPDATED_BY    
_____ ______________ __________ ________ ___________ _______________ __________________________________ _______________ _____________ _____________ 
   25 nisse          bosse      bengt    olle        korv med mos    18-NOV-22 07.19.07.000000000 PM    CONFIGSERVER

但是,如果我使用Spring Data JPA,DEFAULT值由于某种原因没有填充?
My Properties类如下所示:

package com.example.configcrud.model;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "properties")
public class Properties {

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

    @Column(name = "application")
    private String application;

    @Column(name = "profile")
    private String profile;

    @Column(name = "label")
    private String label;

    @Column(name = "prop_key")
    private String propKey;

    @Column(name = "value")
    private String value;

    @Column(name = "created_on", updatable = false)
    private Timestamp createdOn;

    @Column(name = "created_by", updatable = false)
    private String createdBy;

    public Properties(String application, String profile, String label, String propKey, String value) {
         this.application = application;
         this.profile = profile;
         this.label = label;
         this.propKey = propKey;
         this.value = value;
    }

    public Properties() {
    }

    public String getApplication() {
        return application;
    }

    public void setApplication(String application) {
        this.application = application;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getPropKey() {
        return propKey;
    }

    public void setPropKey(String propKey) {
        this.propKey = propKey;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Timestamp getCreatedOn() {
        return createdOn;
    }

    public Timestamp setCreatedOn() {
        return createdOn;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public String setCreatedBy() {
        return createdBy;
    }

    @Override
    public String toString() {
        return "Properties [id=" + id + ", " +
                "application=" + application + ", " +
                "profile=" + profile + ", " +
                "label=" + label +
                "propkey=" + propKey +
                "value=" + value +
                "CreatedOn=" + createdOn +
                "CreatedBy=" + createdBy + "]";
    }
}

我使用的是Spring Data JPA:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

有人知道我可以做些什么来使DEFAULT值被JPA填充或不被覆盖吗?
谢谢安迪

m2xkgtsf

m2xkgtsf1#

默认情况下,JPA会显式插入所有列。为了不插入某个列,而是在插入时从数据库中读取,请使用@Generated注解,如下所示:

@Generated(GenerationTime.INSERT)
@Column(name = "created_on", updatable = false, insertable = false)
private Timestamp createdOn;

相关问题