프레임워크
-
N + 1 정복하기 <2. FetchType.EAGER>프레임워크/Spring Boot 2024. 2. 20. 18:09
(소스코드: https://github.com/blog-example/-JPA-N_Plus_1) ORM을 사용하면 만나는 흔한 이슈 중 하나인 N + 1 ! 명확하게 설명할 수 있을 정도로 머리속에 집어넣어보겠습니다. 지난글에서 N + 1이 무엇이고 왜 발생하고 어떤 코드에서 발생하는지 살펴보았습니다. 이번 글에서는 FetchType.EAGER와 N + 1의 관계에 대해서 살펴보겠습니다. [글의 진행] 1. FetchType.EAGER 적용하기 2. FetchType.EAGER 공식문서 확인하기 3. FetchType.EAGER 실행확인하기 4. FetchType.EAGER의 또 다른 동작 지난 글에서 연관관계가 있는 엔티티 로딩시 성능 최적화를 위해서 FetchType.LAZY를 사용하면서 N + 1 ..
-
N + 1 정복하기 <1. N + 1?>프레임워크/Spring Boot 2024. 2. 20. 16:13
(소스코드: https://github.com/blog-example/-JPA-N_Plus_1) ORM을 사용하면 만나는 흔한 이슈 중 하나인 N + 1 ! 명확하게 설명할 수 있을 정도로 머리속에 집어넣어보겠습니다. 이번글에서는 N + 1이 무엇이고 왜 발생하고 어떤 코드에서 발생하는지 살펴보겠습니다. 1. N + 1이 무엇? 2. 프로젝트 세팅하기 3. N + 1 왜 발생하는가? 1. N + 1 이 무엇? gpt는 n + 1 이슈를 무엇이라고 정의하는지 살펴보겠습니다. 명쾌하게 잘 정리해준 것 같습니다! 면접 때 질문을 받으면 이 정도 내용으로 답변을 하면 좋겠다는 생각이 듭니다. # 데이터베이스 쿼리 최적화 이슈 # 한 번의 쿼리로 N개의 엔티티를 가져오지만 해당 엔티티에 접근할 때 추가로 N번의 ..
-
데이터베이스를 기반으로 JPA의 연관관계 살펴보기 <1>프레임워크/Spring Boot 2024. 2. 6. 20:10
소스코드: https://github.com/blog-example/-JPA-_relationship_mapping 이번에는 아티스트를 새로 등록하는 시나리오를 진행하면서 관련된 Entity 간의 연관관계를 맺는 법을 살펴보겠습니다. 0. 기본 시나리오 생성 우선 연관관계를 생각하지 않고 데이터베이스 레벨에서 테이블을 생성하고, JPA에서 Entity를 만들어보겠습니다. 아티스트는 솔로가수 혹은 아이돌 그룹을 생각해주시면 됩니다. 멤버는 솔로일 경우에 솔로 자신, 그룹일 경우 그룹의 멤버입니다. 연관 관계가 없는 상태의 아티스트와 멤버 ERD 입니다. 이를 표현하는 SQL을 사용해서 데이터베이스에 테이블을 생성해보겠습니다. CREATE TABLE IF NOT EXISTS artists( artist_id..
-
Entity의 Id 생성 전략에 따른 EntityManager의 persist 동작 확인프레임워크/Spring Boot 2024. 2. 6. 19:28
소스코드: https://github.com/blog-example/-JPA-_persist_with_how_to_generate_id (예제에서는 Spring boot web, jpa, lombok, connectorj를 의존성으로 사용하고 있습니다.) EntityManager의 persist의 동작이 예상하던 것과 다르게 작동하는 경우를 만나서 검증을 해보았습니다. 예제 사용될 간단한 Entity를 소개합니다. import jakarta.persistence.*; import lombok.Getter; @Getter @Entity @Table(name = "artists") public class Artist { @GeneratedValue(strategy = GenerationType.IDENTIT..
-
Spring boot 2.7.18에서 swagger 설정하기 <2>프레임워크/Spring Boot 2024. 1. 23. 02:21
프로젝트를 함께 만들고 있는 프론트엔드 개발자들과 효율적인 커뮤니케이션을 위해서 스웨거를 사용하려고 합니다. 적용하면 만난 이런 저런 이슈 사항들을 기록해보았습니다. 프로젝트 버전 spring boot: 2.7.18 springdoc-openapi-ui: 1.7.0 1. 프로젝트 코드에 작성 어노테이션을 사용해서 컨트롤러의 메소드와 관련된 dto들에 명세를 추가해주면 web에서 확인이 가능합니다. api 명세 작성 @Operation(summary = "게시글 생성", description = "제목, 내용, 작성자, 카테고리를 포함하는 게시글을 작성합니다.") @ApiResponses(value = { @ApiResponse(responseCode = "201", description = "게시물 생성..
-
Spring boot 2.7.18에서 swagger 설정하기 <1>프레임워크/Spring Boot 2024. 1. 22. 19:45
프로젝트를 함께 만들고 있는 프론트엔드 개발자들과 효율적인 커뮤니케이션을 위해서 스웨거를 사용하려고 합니다. 적용하면 만난 이런 저런 이슈 사항들을 기록해보았습니다. 프로젝트 버전 spring boot: 2.7.18 springfox: 3.0.0 springdoc-openapi-ui: 1.7.0 요즘은 gpt에게 물어봐서 대략적인 흐름을 파악하고, 한 단계씩 검증하며 적용하는 방법으로 작업을 하는 것 같습니다. 아래 처럼 3단계면 된다고 하는데, 당연히 한번에 안될것이라 예상하고 가보겠습니다. 1. SpringFox 사용 Step1. 의존성 추가하기 아래의 의존성을 추가하기 전에 공식 홈페이지를 한번 찾아가봅니다. implementation 'io.springfox:springfox-boot-starte..
-
mongodb의 @Indexed 가 작동하지 않을 때프레임워크/Spring Boot 2023. 12. 24. 16:41
Entity에 @Indexed 어노테이션이 작동하지 않는 이슈를 해결하는 과정입니다. 아래는 Spring에서 선언한 Entity입니다. productId에 @Indexed(unique = true) 를 적용했음에도 DuplecateKeyError가 발생하지 않았습니다 @Document(collection="products") public class ProductEntity { @Id private String id; @Version private Integer version; @Indexed(unique = true) private int productId; 확인해보니 mongodb에 인덱스가 설정되지 있지 않았습니다 공식문서를 확인해보면 3.0 버전부터 의도하지 않은 impact를 방지하기 위해 명시적..
-
Junit5 에서 Junit4로 변경하기프레임워크/Spring Boot 2023. 12. 24. 14:44
1. 의존성 변경 스프링에서 junit 테스트를 실행하기 위해서는 아래와 같은 의존성을 추가해서 사용합니다. testImplementation 'org.springframework.boot:spring-boot-starter-test' 위 의존성은 Junit5를 기준으로 작동하게 되어있기 때문에 Junit4를 사용하고 싶으면 수정이 필요합니다. spring-boot-starter-test는 여러가지 패키지들을 조합해서 사용하는데, 대표 적으로 Junit5와 관련된 패키지는 아래와 같습니다. junit-jupiter junit-platform-commons 따라서 Junit4를 사용하고 싶다면 위의 패키지들을 제외하고, 추가로 Junit4를 의존성에 추가해주면 사용가능합니다. testImplementati..