JPA 중복 컬럼 상속으로 생성하는 방법
- Coding/JPA
- 2022. 1. 16.
반응형
728x90
반응형
상황
Member, Category, Member_Category 총 3개의 테이블이 있다.
위 테이블을 보면 아래 컬럼 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)
)
반응형
'Coding > JPA' 카테고리의 다른 글
[JPA] 복합키 - 비식별관계 매핑하기 (@IdClass, @EmbeddedId) (0) | 2022.02.09 |
---|---|
[JPA] 고급매핑 - 조인 전략, 단일 테이블 전략, 구현 클래스별 테이블 전략 (0) | 2022.02.07 |
[SpringBoot JPA] Paging 페이징 (0) | 2021.10.26 |
[SpringBoot JPA] Limit, Order 처리하기 (0) | 2021.10.26 |
[SpringBoot JPA] Like 데이터 조회 (2) | 2021.10.26 |