본문 바로가기
JPA

JPA Entity에 index 거는 방법

by 문자메일 2022. 11. 26.

@Table 어노테이션안에 아래처럼 @Index 부분 참조해서 작성하면 인덱스 자동으로 걸리게 됨

@Entity
@Table(name = "\"comment\"", indexes ={
        @Index(name = "post_id_idx", columnList = "post_id")
})
@Getter
@Setter
//@SQLDelete(sql = "UPDATE \"comment\" SET deleted_at = NOW() where id=?")
//@Where(clause = "delete_at is NULL")
public class CommentEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private UserEntity user;

    @ManyToOne
    @JoinColumn(name = "post_id")
    private PostEntity post;

    @Column(name = "comment")
    private String comment;

    @Column(name = "registered_at")
    private Timestamp registeredAt;

    @Column(name = "updated_at")
    private Timestamp updatedAt;

    @Column(name = "deleted_at")
    private Timestamp deletedAt;


    //@PrePersist
    void registeredAt(){
        this.registeredAt = Timestamp.from(Instant.now());
    }

    //@PrePersist
    void updatedAt(){
        this.updatedAt = Timestamp.from(Instant.now());
    }

    public static CommentEntity of(UserEntity userEntity, PostEntity postEntity, String comment){
        CommentEntity entity = new CommentEntity();
        entity.setUser(userEntity);
        entity.setPost(postEntity);
        entity.setComment(comment);

        return entity;
    }
}

댓글