개발 무지렁이

[Spring Boot] 여분의 필드 extra 본문

Backend/스프링부트

[Spring Boot] 여분의 필드 extra

Gaejirang-e 2023. 1. 2. 16:35

LinkedHashMap


입력된 순서가 보장되는 HashMap

[base.entity.BaseEntity.java]

@Getter
@SuperBuilder
@MappedSuperclass
@NoArgsConstructor(access = PROTECTED)
@EntityListeners(AuditingEntityListener.class)
@ToString
public class BaseEntity {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private Long id;
    @CreatedDate
    private LocalDateTime createDate;
    @LastModifiedDate
    private LocalDateTime modifyDate;
    @Transient // 아래 필드가 DB 필드가 되는 것을 막는다.
    @Builder.Default
    private Map<String, Object> extra = new LinkedHashMap<>();

    public BaseEntity(long id) {
        this.id = id;
    }
}

[ArticleService.java]

public Article getForPrintArticleById(Long id) {
    Article article = getArticleById(id);
    article.getExtra().put("age", 22);
    return article;
}
Comments