티스토리 뷰
@MappedSuperclass
예시
상황가정
이 애플리케이션은 모든 곳에 등록, 수정과 관련된 정보가 항상 있어야 한다.
이 때 Member에
private String createdBy;
private LocalDateTime createdDate;
private String lastModifiedBy;
private LocalDateTime lastModifiedDate;
써 줘도 되지만 번거롭고 중복되므로
BaseEntity로 묶어서 새로 하나 만들고, @MappedSuperClass 넣어주고,
나머지 클래스는 이 클래스를 상속받게 만든다.
BaseEntity
@MappedSuperclass // 매핑 정보만 받는 부모클래스
public class BaseEntity {
private String createdBy;
private LocalDateTime createdDate;
private String lastModifiedBy;
private LocalDateTime lastModifiedDate;
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public LocalDateTime getCreatedDate() {
return createdDate;
}
public void setCreatedDate(LocalDateTime createdDate) {
this.createdDate = createdDate;
}
public String getLastModifiedBy() {
return lastModifiedBy;
}
public void setLastModifiedBy(String lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
public LocalDateTime getLastModifiedDate() {
return lastModifiedDate;
}
public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
}
Member 변경
@Entity
@NoArgsConstructor
public class Member extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "MEMBER_ID")
private Long id;
//양방향 추가(역방향)
@ManyToOne
@JoinColumn(name = "TEAM_ID",insertable = false, updatable = false) // 읽기 전용화
private Team team;
@Column(name = "USERNAME")
private String username;
@OneToOne
@JoinColumn(name = "LOCKER_ID")
private Locker locker;
@OneToMany(mappedBy = "member")
private List<MemberProduct> memberProducts = new ArrayList<>();
// private String createdBy;
// private LocalDateTime createdDate;
// private String lastModifiedBy;
// private LocalDateTime lastModifiedDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Team 변경
@Entity
public class Team extends BaseEntity {
@Id
@GeneratedValue
@Column(name = "TEAM_ID")
private Long id;
private String name;
// 여기가 이제 연관관계를 관맇라기 위해 추가
@OneToMany
// @JoinColumn(name = "TEAM_ID")
private List<Member> members = new ArrayList<>();
// private String createdBy;
// private LocalDateTime createdDate;
// private String lastModifiedBy;
// private LocalDateTime lastModifiedDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Member> getMembers() {
return members;
}
public void setMembers(List<Member> members) {
this.members = members;
}
}
JpaMain 에도 값 좀 다르게 집어넣으면
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
Member member = new Member();
member.setUsername("user1");
member.setCreatedBy("kim");
member.setCreatedDate(LocalDateTime.now());
em.persist(member);
tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
emf.close();
}
}
실행해보면
Hibernate:
create table Member (
MEMBER_ID bigint not null,
createdBy varchar(255),
createdDate timestamp,
lastModifiedBy varchar(255),
lastModifiedDate timestamp,
USERNAME varchar(255),
LOCKER_ID bigint,
TEAM_ID bigint,
primary key (MEMBER_ID)
)
Hibernate:
create table Team (
TEAM_ID bigint not null,
createdBy varchar(255),
createdDate timestamp,
lastModifiedBy varchar(255),
lastModifiedDate timestamp,
name varchar(255),
primary key (TEAM_ID)
)
결국 @MappedSuperclass 속성은
상속관계 매핑 이런 게 아니고
여기 있는 속성을 공통적으로 그냥 가져다 쓰는 것.
'[개발] - Spring > JPA 공부' 카테고리의 다른 글
프록시 (0) | 2023.08.24 |
---|---|
실전 예제 - 4. 상속관계 매핑 (0) | 2023.08.23 |
상속관계 매핑 (0) | 2023.08.23 |
실습 3 - 다양한 연관관계 매핑 (1) | 2023.08.22 |
다대다 [N : N] (0) | 2023.08.21 |