Skip to content

Commit

Permalink
Feat: spring data jpa - InheritanceType.JOINED
Browse files Browse the repository at this point in the history
  • Loading branch information
bohub12 committed Sep 10, 2023
1 parent fbed8da commit beda596
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 2 deletions.
35 changes: 33 additions & 2 deletions spring-data-jpa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,46 @@
- 사용되는 어노테이션
- `@PrimaryKeyJoinColumn, @DiscriminatorColumn, @DiscriminatorValue`

```java
// Item.java
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "DTYPE")
public abstract class Item {

@Id @GeneratedValue
private long id;

private String name;
private int price;
}

// Album.java
@Entity
@DiscriminatorValue("A")
public class Album extends Item {

private String artist;
}

// Book.java
@Entity
@DiscriminatorValue("B")
public class Book extends Item {

private String author;
}
```

### 2. 단일테이블 전략 (`InheritanceType.SINGLE_TABLE`)
- 이름 그래도 테이블을 하나만 사용하는 방식이다
- 또한 `조인전략`과 마찬가지로 구분칼럼(`DTYPE`)을 이용해서 어떤 자식데이터가 저장되었는지 구분한다
- 장점
- 조회할 때 조인을 사용하지 않으므로 일반적으로 가장 빠르다
- 조회할 때 조인을 사용하지 않으므로 가장 빠르다
- 조회 쿼리가 단순하다
- 단점
- 자식 엔티티가 매핑한 컬럼은 모두 null 허용해야한다
- 한 테이블에 모두 저장하므로 테이블이 의도치않게 커질 수 있다. 변경과 확장에 취약해지게 된다.
- 한 테이블에 모두 저장하므로 테이블이 의도치않게 커질 수 있다. 변경과 확장에 취약해지게 된다.

```java
// Item.java
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.demo.controller;

import com.example.demo.domain.joined.JoinedAlbum;
import com.example.demo.domain.singletable.SingleTableAlbum;
import com.example.demo.domain.tableperclass.TablePerClassAlbum;
import lombok.AllArgsConstructor;
Expand All @@ -22,4 +23,8 @@ public SingleTableAlbum toSingleTableEntity() {
public TablePerClassAlbum toTablePerClassEntity() {
return new TablePerClassAlbum(name, price, artist);
}

public JoinedAlbum toJoinedEntity() {
return new JoinedAlbum(name, price, artist);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.demo.controller;

import com.example.demo.controller.dto.joined.GetJoinedAlbumDto;
import com.example.demo.controller.dto.singletable.GetSingleTableAlbumDto;
import com.example.demo.controller.dto.tableperclass.GetTablePerClassAlbumDto;
import com.example.demo.domain.joined.JoinedAlbum;
import com.example.demo.domain.singletable.SingleTableAlbum;
import com.example.demo.domain.singletable.SingleTableItem;
import com.example.demo.domain.tableperclass.TablePerClassAlbum;
import com.example.demo.repository.joined.JoinedAlbumRepository;
import com.example.demo.repository.singletable.SingleTableAlbumRepository;
import com.example.demo.repository.tableperclass.TablePerClassAlbumRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,6 +19,7 @@ public class TestController {

private final SingleTableAlbumRepository singleTableAlbumRepository;
private final TablePerClassAlbumRepository tablePerClassAlbumRepository;
private final JoinedAlbumRepository joinedAlbumRepository;

@PostMapping("/single/album")
public long createSingleTableAlbum(@RequestBody CreateAlbumDto body) {
Expand All @@ -29,6 +33,12 @@ public long createTablePerClassAlbum(@RequestBody CreateAlbumDto body) {
return entity.getId();
}

@PostMapping("/joined/album")
public long createJoinedAlbum(@RequestBody CreateAlbumDto body) {
JoinedAlbum entity = joinedAlbumRepository.save(body.toJoinedEntity());
return entity.getId();
}

@GetMapping("/single/album")
public GetSingleTableAlbumDto getSingleTableAlbum(@RequestParam long id) {
return new GetSingleTableAlbumDto(singleTableAlbumRepository.findById(id).orElseThrow());
Expand All @@ -38,4 +48,9 @@ public GetSingleTableAlbumDto getSingleTableAlbum(@RequestParam long id) {
public GetTablePerClassAlbumDto getTablePerClassAlbum(@RequestParam long id) {
return new GetTablePerClassAlbumDto(tablePerClassAlbumRepository.findById(id).orElseThrow());
}

@GetMapping("/joined/album")
public GetJoinedAlbumDto getJoinedAlbum(@RequestParam long id) {
return new GetJoinedAlbumDto(joinedAlbumRepository.findById(id).orElseThrow());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.demo.controller.dto.joined;

import com.example.demo.domain.joined.JoinedAlbum;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class GetJoinedAlbumDto {
private long id;
private String name;
private int price;
private String artist;

public GetJoinedAlbumDto(JoinedAlbum entity) {
this.id = entity.getId();
this.name = entity.getName();
this.price = entity.getPrice();
this.artist = entity.getArtist();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.demo.domain.joined;

import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Getter
@ToString
@NoArgsConstructor
@DiscriminatorValue("A")
public class JoinedAlbum extends JoinedItem {

private String artist;

public JoinedAlbum(String name, int price, String artist) {
super(name, price);
this.artist = artist;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.demo.domain.joined;

import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;

@Entity
@DiscriminatorValue("B")
public class JoinedBook extends JoinedItem {

private String author;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.demo.domain.joined;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Getter
@ToString
@NoArgsConstructor
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "DTYPE")
public abstract class JoinedItem {

@Id @GeneratedValue
private long id;

private String name;
private int price;

public JoinedItem(String name, int price) {
this.name = name;
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.demo.domain.joined;

import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;

@Entity
@DiscriminatorValue("M")
public class JoinedMovie extends JoinedItem {
private String director;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.demo.repository.joined;

import com.example.demo.domain.joined.JoinedAlbum;
import org.springframework.data.jpa.repository.JpaRepository;

public interface JoinedAlbumRepository extends JpaRepository<JoinedAlbum, Long> {
}

0 comments on commit beda596

Please sign in to comment.