0

我正在做一个项目,我需要定期用行填充一个四列表。数据库是postgresql。这些列是 string1、date1、string2、date2。我已经用 SQLAlchemy 在 python 中测试了这个项目,我在初始插入时填充了 string1 和 date1,后来填充了 string2 和 date2。由于项目逻辑,string2 和 date2 在插入时不可用,并且可能会在初始插入后数周更新。使用 SQLAlchemy,我可以简单地执行如下插入操作:

row = Row(
string1 = "SomeString",
date1 = datetime.now()
)

我也可以这样做:

row = Row(
string1 = "SomeString",
date1 = datetime.now(),
string2 = None,
date2 = None
)

现在,我需要在 Java Spring 中创建项目(我没有太多经验),并且 CrudRepository.save() 方法不允许我以上述任何一种方式进行。它坚持必须为每一列设置某种值(对应于正确的数据类型,截至目前我使用 LocalDate 作为日期,但任何 Date 数据类型都可以)。我现在的解决方法是设置 string2 = "" 和 date2 = LocalDate.parse("0001-01-01"),但这是一个低于标准的解决方案。我想发送一个 Null/None 值,以便 postgres 将其插入为 .

目前实体:

@Entity
@DynamicUpdate
@Table(name = "subsets", schema = "public")
class Subsets(
    @Id
    val id: String,
    val author_created: String,
    val date_created: LocalDate,
    val author_committed: String,
    val date_committed: LocalDate,
) {
   private constructor() : this("", "", LocalDate.now(), "", LocalDate.now())
} 

发帖逻辑:

@PostMapping("/createsubset")
fun createSubset(
    @RequestParam id: String,
    @RequestParam author_created: String,
    ){
    subsetsRepository.save(
        Subsets(
            id = id,
            author_created = author_created,
            date_created = LocalDate.now(),
            author_committed = "",
            date_committed = LocalDate.parse("0001-01-01")
        )
    )

}
4

1 回答 1

0

问题在于您定义的模型类。创建一个像下面这样的模型类,您可以轻松地保存数据。

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

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

@Entity
@Table(name = "subsets")
public class Subset {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "author_created", nullable = false)
    private String authorCreated;

    @Column(name = "date_created", updatable = false)
    @CreationTimestamp
    private Timestamp dateCreated;

    @Column(name = "author_committed")
    private String authorCommitted;

    @Column(name = "date_committed")
    @UpdateTimestamp
    private Timestamp dateCommitted;

    public Subset() {
    }

    public Subset(int id, String authorCreated) {
        this.id = id;
        this.authorCreated = authorCreated;
    }

    public int getId() {
        return id;
    }

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

    public String getAuthorCreated() {
        return authorCreated;
    }

    public void setAuthorCreated(String authorCreated) {
        this.authorCreated = authorCreated;
    }

    public Timestamp getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Timestamp dateCreated) {
        this.dateCreated = dateCreated;
    }

    public String getAuthorCommitted() {
        return authorCommitted;
    }

    public void setAuthorCommitted(String authorCommitted) {
        this.authorCommitted = authorCommitted;
    }

    public Timestamp getDateCommitted() {
        return dateCommitted;
    }

    public void setDateCommitted(Timestamp dateCommitted) {
        this.dateCommitted = dateCommitted;
    }
}
于 2021-10-25T10:31:43.423 回答