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

Kotlin Spring Boot:如何让CrudRepository忽略实体类属性?

如何让CrudRepository生成数据库表时忽略实体类字段?

完全可行,你可以使用JPA规范中的@Transient注解标记需要忽略的字段,Spring Data JPA(包括CrudRepository)会识别这个注解,在生成数据库表时跳过这些字段的映射。

修改你的实体类,给不需要持久化的字段加上@Transient注解即可:

import javax.persistence.Id
import javax.persistence.Table
import javax.persistence.Transient

@Table
class Property(
    @Id val id: String?,
    val name: String,
    @Transient val objectType: String,
    @Transient val currentValue: String?
)

注解说明

@Transient是JPA的标准注解,它的作用是明确告诉ORM框架(Spring Data JPA默认基于Hibernate实现):这个字段不需要映射到数据库表中的任何列,因此在自动建表时不会为这些字段生成对应列,执行CRUD操作时也不会处理它们。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 07:04:56