TIL_230627

์ง€์ธยท2023๋…„ 6์›” 27์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
28/38
post-custom-banner

๐Ÿฐ @Bean, @Component, @Configuration๊ณผ @Transactional์„ ์ •๋ฆฌํ–ˆ๋‹ค.

[String] @Bean @Component

[JPA] @Tranactional๊ณผ ๋กค๋ฐฑ

๐Ÿฐ Lv1 ๊ฐœ์ธ๊ณผ์ œ ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•„์„œ ํ”ผ๋“œ๋ฐฑ์— ๋Œ€ํ•œ ๋‚ด์šฉ๋“ค์„ ์ •๋ฆฌํ•ด ๋ณด๋ ค ํ•œ๋‹ค.


๐Ÿพ @Transactional

  1. repository์˜ createPost method๋ฅผ ์‚ฌ์šฉํ•  ๋• ์™œ Transactional ์–ด๋…ธํ…Œ์ด์…˜์ด ์—†์–ด๋„ ๋˜์—ˆ์„๊นŒ์š”?
public PostResponseDto createPost(PostRequestDto requestDto) {
        // RequestDto -> Entity
        Post post = new Post(requestDto);

        // DB ์ €์žฅ
        Post savePost = postRepository.save(post);

        // Entity -> ResponseDto
        PostResponseDto postResponseDto = new PostResponseDto(post);

        return postResponseDto;
    }
  • ์œ„ ๋ฉ”์†Œ๋“œ๋Š” ์ƒˆ๋กœ์šด ๊ฒŒ์‹œ๋ฌผ์„ ์ƒ์„ฑํ•˜๊ณ  ์ €์žฅํ•˜๋Š” Transactional ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์€ ๋ฉ”์†Œ๋“œ์ด๋‹ค.

  • ํŠœํ„ฐ๋‹˜์˜ ํ”ผ๋“œ๋ฐฑ์„ ๋ณด๊ณ  ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด์•˜๋Š”๋ฐ ์ƒ์„ฑํ•˜๊ณ  ์ €์žฅํ•˜๋Š” ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ณ€๊ฒฝ์„ ๊ฐ€ํ•ด์ง€๋Š” ์ž‘์—…์ด์˜€๋‹ค.

  • ๋•Œ๋ฌธ์— Transactional ์–ด๋…ธํ…Œ์ด์…˜์ด ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•œ๋‹ค.

  1. ๋˜ํ•œ ์กฐํšŒํ•ด์˜ค๋Š” method์—๋Š” ์™œ ๋ถ™์ด์ง€ ์•Š์•˜๋Š”์ง€, ํ•„์š”ํ•˜์ง„ ์•Š์„์ง€ ์–ด๋…ธํ…Œ์ด์…˜์˜ ์˜๋ฏธ์™€ ์—ญํ• ์„ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.
  • ์กฐํšŒ ๋ฉ”์„œ๋“œ๋Š” ์กฐํšŒ ์ž‘์—…๋งŒ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ํŠธ๋žœ์žญ์…˜์„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

[JPA] @Tranactional๊ณผ ๋กค๋ฐฑ

  • ์˜๋ฏธ์™€ ์—ญํ• ์„ ์ •๋ฆฌํ•ด ๋ณด์•˜๋Š”๋ฐ ์ •๋ฆฌํ•˜๊ธฐ ์ „์—๋Š” ๋‘๋ฃจ๋ญ‰์‹คํ•˜๊ฒŒ๋งŒ ์•Œ๊ณ  ์จ์„œ ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์€ ์‹ค์ˆ˜๋ฅผ ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.


๐Ÿพ @Temporal

  1. ํ•„์ˆ˜๋กœ ๋ถ™์—ฌ์•ผํ•  ์–ด๋…ธํ…Œ์ด์…˜์ผ๊นŒ์š”? ์ด ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต์„ ์ฐพ์•„๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค!
    @CreatedDate
    @Column(updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private LocalDateTime createdAt;
  • ์™œ ํŠœํ„ฐ๋‹˜์ด ์ด๋Ÿฐ ํ”ผ๋“œ๋ฐฑ์„ ์ฃผ์—ˆ๋Š”์ง€ Temporal ์–ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด๋ฉด์„œ ๋‹ต์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    • Java 8๋ถ€ํ„ฐ LocalDateTime์ด ์ง€์›๋ผ Temporal ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋งคํ•‘ํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง„ ๊ฒƒ์ด๋‹ค.
	@CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdAt;
  • ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑ ํ•˜๋ฉด CreatedDate ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด createdAt ํ•„๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ๊ด€๋ฆฌ๋˜๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ž๋™์œผ๋กœ ๊ธฐ๋กํ•˜๊ฒŒ ๋œ๋‹ค.


๐Ÿพ @RequiredArgsConstructor

  1. **๋‹˜์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ์‹  ์–ด๋…ธํ…Œ์ด์…˜์ด์—์š”!
    ์–ด๋–ค ์—ญํ• ์„ ํ•˜๊ณ , ์ž‘์„ฑํ•˜์‹  ์ฝ”๋“œ์—์„œ ์–ด๋–ค ๋ถ€๋ถ„์„ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•ด๋ณด์„ธ์š”. :)
@Getter
public class PostResponseDto {
    private Long id;
    private String title;
    private String username;
    private String content;
    private String password;
    private LocalDateTime createdAt;
    private LocalDateTime modifiedAt;

    public PostResponseDto(Post post) {
        this.id = post.getId();
        this.username = post.getUsername();
        this.title = post.getTitle();
        this.content = post.getContent();
        this.password = post.getPassword();
        this.createdAt = post.getCreatedAt();
        this.modifiedAt = post.getModifiedAt();
    }
}
  • ์œ„ ์ฝ”๋“œ์—์„œ RequiredArgsConstructor ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์•„์ง„๋‹ค.
@Getter
@RequiredArgsConstructor
public class PostResponseDto {
    private final Long id;
    private final String title;
    private final String username;
    private final String content;
    private final String password;
    private final LocalDateTime createdAt;
    private final LocalDateTime modifiedAt;
}
  • @RequiredArgsConstructor์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•„๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด ์ค€๋‹ค.

  • ์ฃผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

    • ์ฃผ์ž…ํ•ด์•ผ ํ•˜๋Š” ์˜์กด์„ฑ๋“ค์„ final ์ด๋‚˜ @NonNull ์œผ๋กœ ์„ ์–ธํ•˜๊ณ , ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ํ•„๋“œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
profile
์—ด์ฉก
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€