Lombok을 적용하기 위해서 다음과 같이 Lombok dependency를 추가하고 Compiler 설정에 Lombok Annotation Processor를 추가합니다.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <properties> <java.version>1.8</java.version> <maven.compiler.version>3.8.1</maven.compiler.version> <spring.boot.version>2.4.1</spring.boot.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>UTF-8</encoding> <annotationProcessors> <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor </annotationProcessor> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
IntelliJ IDEA에서 Lombok Plugin을 설치한 후 다음의 코드를 작성합니다.
import lombok.*; import javax.persistence.Column; import javax.persistence.Embeddable; import java.io.Serializable; import java.util.Objects; @Getter @Setter @NoArgsConstructor @Builder @AllArgsConstructor @Embeddable public class UserId implements Serializable { @Column(name = "username", columnDefinition = "VARCHAR(100)") String username; @Column(name = "path", columnDefinition = "VARCHAR(255)") String path; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UserId userId = (UserId) o; return Objects.equals(username, userId.username) && Objects.equals(path, userId.path); } @Override public int hashCode() { return Objects.hash(username, path); } } import com.fasterxml.jackson.annotation.JsonFormat; import lombok.*; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import java.sql.Timestamp; @Getter @Setter @NoArgsConstructor @Builder @AllArgsConstructor @Entity(name = "api_iam_user") public class User { @EmbeddedId UserId userId; @Column(name = "tags", columnDefinition = "VARCHAR(255)") String tags; @Column(name = "permission_boundary", columnDefinition = "VARCHAR(255)") String permissionBoundary; /** * 생성일 (이 필드에는 값을 입력하지 않아도 Hibernate가 INSERT시 자동으로 기록) */ @CreationTimestamp @Column(name = "create_ts", insertable = true, updatable = false) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss") Timestamp createTime; /** * 변경일 (이 필드에는 값을 입력하지 않아도 Hibernate가 UPDATE시 자동으로 기록) */ @UpdateTimestamp @Column(name = "update_ts", insertable = true, updatable = true) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss") Timestamp updateTime; }