Spring 数据 JDBC - @Column 注释在 setter 上不起作用

Spring Data JDBC - @Column annotation does't work on setter

我需要在获取时转换实体的字段,根据 this official example 我已经尝试使用自定义 setter:

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;


@Table("entity")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(builderClassName = "EntityBuilder")
public class Entity {

    @Id
    private String someId;

    @Transient
    private String entityName;

    @Column("entity_name")
    public String getEntityNameUnmodified() {
        return this.entityName;
    }

    @Column("entity_name")
    public void setEntityNameUnmodified(String em) {
        this.entityName = em + " Some modification";
    }
}

但这完全行不通,结果我得到 EntityentityName == null

我已经在本地下载了 GitHub 示例和 运行 并且一切正常。我的代码有什么问题?

由于缺少 @AccessType(AccessType.Type.PROPERTY) 注释,我的代码无法运行。

工作解决方案:

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;


@Table("entity")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(builderClassName = "EntityBuilder")
@AccessType(AccessType.Type.PROPERTY) // IMPORTANT !!!
public class Entity {

    @Id
    private String someId;

    @Transient
    private String entityName;

    @Column("entity_name")
    public String getEntityNameUnmodified() {
        return this.entityName;
    }

    @Column("entity_name")
    public void setEntityNameUnmodified(String em) {
        this.entityName = em + " Some modification";
    }
}

问题是 Spring Data JDBC 默认使用字段作为实体列的访问器,如果没有注释,设置器和获取器将被忽略。启用 PROPERTY 访问类型可以解决问题。