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

@Transient注解失效求助:Spring Boot Data字段仍被数据库映射

Spring Boot Data @Transient注解失效排查问题

我知道Spring Boot Data里的@Transient注解是用来让数据库映射忽略标注字段的,但现在这个功能失效了(不确定之前是否正常工作,这是我正在开发的新功能)。我试过在getter方法上也加这个注解,结果还是不行。

相关代码

//imports... 
//import javax.persistence.Transient;//--->also tried
import org.springframework.data.annotation.Transient;

import lombok.Data;

@Entity
@Data
public class AppUser extends User{

    //...

    @Transient//the JPA should not mapping this field
    private String[] passwordCopies= new String[2];

    @Transient
    private String excludeMe = "aaa";
    
    @Transient
    public String getExcludeMe() {
        return excludeMe;
    }

    //...

} 

数据库结构截图

DB scheme

排查步骤

  • 修正注解导入:JPA用来忽略数据库映射的注解是javax.persistence.Transient(Spring Boot 2.x)或jakarta.persistence.Transient(Spring Boot 3.x),你当前导入的org.springframework.data.annotation.Transient是Spring Data用于查询投影忽略的注解,和JPA实体映射无关,这是核心问题,先替换成正确的注解。
  • 检查Lombok冲突:@Data会自动生成getter/setter,你手动编写了getExcludeMe方法,可能导致注解生效异常。建议删除手动编写的getter,只在字段上添加正确的@Transient注解测试。
  • 确认实体扫描配置:确保AppUser所在包被@EntityScan注解覆盖,Spring Boot能正确加载该实体类的配置。
  • 验证DDL配置:检查application.properties/application.yml中spring.jpa.hibernate.ddl-auto的配置值(如create/update),确认Hibernate自动建表逻辑生效;如果是手动建表,确认表中字段是否是之前手动添加的,而非JPA生成。
  • 清理缓存重启:清除Hibernate缓存及应用缓存后重启服务,避免旧的映射配置残留。
  • 检查父类User:确认父类中没有同名字段或冲突的映射配置,子类字段可能受父类配置影响。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 00:22:15