You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Spring Boot JPA Auditing插入MySQL记录时created_at和updated_at为null

Spring Data JPA审计字段created_at/updated_at填充失败问题

使用Kotlin和Spring Boot开发REST API,Country实体包含用于记录创建时间和最后修改时间的审计字段created_at和updated_at。已在启动类添加@EnableJpaAuditing注解,也尝试过在实体类添加@EntityListeners(AuditingEntityListener::class),但调用插入接口后,数据库中这两个字段仍为null。

代码实现

/model/Country.kt

(注:曾尝试添加@EntityListeners(AuditingEntityListener::class)但无效)

package com.example.example.model

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EntityListeners
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.Table
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.sql.Timestamp


@Entity
@Table(name = "country")
class Country (
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "country_id")
    val id: Long,
    val code: String,
    val displayName: String,

    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    val createdAt: Timestamp,

    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    val updatedAt: Timestamp
)

/controller/CountryController.kt

package com.example.example.controller

import com.example.example.model.Country
import com.example.example.repository.CountryRepository
import jakarta.validation.Valid
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController

import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PostMapping

@RestController
class CountryController {

    @Autowired
    lateinit var countryRepository: CountryRepository

    @PostMapping("/countries", consumes = arrayOf(MediaType.APPLICATION_JSON_VALUE))
    fun createCountry(@Valid @RequestBody country: Country): Country {
        return countryRepository.save(country);
    }
}

ExampleApplication.kt

package com.example.example

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.jpa.repository.config.EnableJpaAuditing

@SpringBootApplication
@EnableJpaAuditing
class ExampleApplication
fun main(args: Array<String>) {
    runApplication<ExampleApplication>(*args)
}

Country表结构

country_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
code VARCHAR(3) NOT NULL,
displayName VARCHAR(40) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (country_id)

请求体JSON(请求头已包含正确Content-Type)

{
    "code":  "FRA",
    "displayName": "France"
}

插入后数据库记录

{
    "id": 1,
    "code": "FRA",
    "displayName": "France",
    "createdAt": null,
    "updatedAt": null
}

疑问点

按照理解,由于未使用@CreatedBy和@LastModifiedBy审计用户信息,无需创建Auditable类。预期@CreatedDate和@LastModifiedDate会在插入前自动填充时间,但实际为null,想知道遗漏了什么配置。

另外,尝试用Hibernate的@CreationTimestamp和@UpdateTimestamp替代,这两个注解能正常填充时间,但更倾向于使用Spring Data的注解,因为未来可能会用到@CreatedBy和@LastModifiedBy,希望了解Spring Data注解无效的原因。

application.properties配置:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect

内容的提问来源于stack exchange,提问作者Dexter Fung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 01:30:30