JPA 중복 컬럼 상속으로 생성하는 방법

반응형
728x90
반응형

상황

Member, Category, Member_Category 총 3개의 테이블이 있다.

 

ERD

 

위 테이블을 보면 아래 컬럼 3개가 중복된다.

1) reg_dts
2) mod_dts
3) is_deleted

 

 

 

 

JPA 클래스 상속으로 중복 컬럼 처리

BaseEntity.java

package com.api.plan.entity.base;

import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class BaseEntity {
    private String regDts;
    private String modDts;
    private String isDeleted;
}

 

Member.java

package com.api.plan.entity;

import com.api.plan.entity.base.BaseEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Member extends BaseEntity {
    @Id
    @GeneratedValue
    private Long idx;

    @Column(unique = true)
    private String memberId;

    private String memberName;

    private String email;

    private String phone;

    private String gender;

    private String age;

    private String jobName;

    /*
      create table member_category (
        member_idx int8 not null,
        category_idx int8 not null,
        primary key (member_idx, category_idx)
    )
     */
    @ManyToMany
    private Set<Category> category = new HashSet<>();
}

 

Category.java

package com.api.plan.entity;

import com.api.plan.entity.base.BaseEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Category extends BaseEntity {
    @Id
    @GeneratedValue
    private Long idx;

    @Column(unique = true, nullable = false)
    private String categoryName;
}

 

생성 쿼리

create table category (
        idx int8 not null,
        is_deleted varchar(255),
        mod_dts varchar(255),
        reg_dts varchar(255),
        category_name varchar(255) not null,
        primary key (idx)
    )
    
create table member (
        idx int8 not null,
        is_deleted varchar(255),
        mod_dts varchar(255),
        reg_dts varchar(255),
        age varchar(255),
        email varchar(255),
        gender varchar(255),
        job_name varchar(255),
        member_id varchar(255),
        member_name varchar(255),
        phone varchar(255),
        primary key (idx)
    )
    
 create table member_category (
        member_idx int8 not null,
        category_idx int8 not null,
        primary key (member_idx, category_idx)
    )

 

 

반응형

Designed by JB FACTORY