【问题标题】:Boot spring hibernate+jpa not returning database level generated column启动spring hibernate + jpa不返回数据库级别生成的列
【发布时间】:2021-01-10 20:20:19
【问题描述】:

我的 Spring Boot 应用程序中有以下实体,它由 SQL Server 数据库支持。

@Entity
@Table(name="people")
public class Person {
    @Id
    @GeneratedValue(strategy=TABLE)
    private Long id;
    
    @Column(
        columnDefinition="datetime default current_timestamp",
        nullable=false, updatable=false, insertable = false
    )
    private String strTimestamp;
    // ...
}

使用 JpaRepository 调用 save 并生成 id 但 strTimestamp 为空,即使我立即调用 getOne 或调用(本机查询)select * from people where id = ?,使用新返回的 Id,该列仍然为空。

有趣的是,如果我对先前插入的行的 id 进行硬编码,它会返回该记录。

我想在插入后返回整行,因为我需要生成的列。

我怎样才能做到这一点?

谢谢。

【问题讨论】:

    标签: sql-server spring-boot hibernate spring-data-jpa


    【解决方案1】:

    将 strTimestamp 数据类型更改为 java.util.Date 并尝试。也使用@Temporal注解

    @Temporal(TemporalType.TIMESTAMP)
    @Column( columnDefinition="datetime default current_timestamp", nullable=false, updatable=false, insertable = false ) private String strTimestamp;
    

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多