Loading the catalog…
Loading the catalog…
Spring / JPA TIL 1. 오류 해결 Memo 생성자 오류 RequestDto 를 받는 생성자가 없어서 발생. public Memo(RequestDto requestDto) { this.title = requestDto.getTitle(); this.contents = requestDto.getContents(); } IntelliJ: Alt + Insert → Constructor Ctrl + Alt + L → 코드 정렬 Ctrl + Shift + F → 프로젝트 전체 검색 Ctrl + F → 현재 파일 검색 POST 404 오류 Controller와 요청 URL이 달라서 발생. Controller: POST /api/memo 요청: POST /api/memos → API 요청 경로와 Controller의 매핑 경로를 동일하게 맞춰야 함. 2. JDBC JDBC는 Java에서 데이터베이스와 통신하기 위한 표준 API. Java → JDBC → DB Driver → Database 직접 SQL을 작성하고 Connection, Statement, ResultSet 등을 관리해야 함. 3. 3 Layer Architecture Controller → Service → Repository → Database Controller : HTTP 요청/응답 담당 Service : 비즈니스 로직 담당 Repository : DB 접근 담당 4. IoC / DI / Bean IoC 객체의 생성과 관리에 대한 제어권을 개발자가 아닌 Spring이 관리하는 것 . DI 필요한 객체를 직접 생성하지 않고 외부(Spring)에서 주입받는 것 . public MemoService(MemoRepository memoRepository) { this.memoRepository = memoRepository; } 생성자 주입을 주로 사용한다. Bean Spring Container가 생성하고 관리하는 객체. Spring Container → Bean 생성/관리 → 필요한 곳에 DI 5. ORM / JPA ORM Java 객체와 관계형 데이터베이스를 매핑하는 기술. JPA Java ORM을 위한 표준 명세 . Hibernate JPA를 구현한 대표적인 구현체. Spring Data JPA → JPA → Hibernate → JDBC → Database 6. Entity DB 테이블과 매핑되는 Java 객체. @Entity public class Memo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private String contents; } @Entity → Entity 지정 @Id → PK 지정 @GeneratedValue → ID 자동 생성 DB Row 하나 ↔ Entity 객체 하나 로 이해하면 됨. 7. 영속성 컨텍스트 JPA가 Entity를 관리하는 논리적인 공간. EntityManager → Persistence Context → Entity 관리 주요 기능: 1차 캐시 Dirty Checking Entity 생명주기 관리 Entity 상태 비영속 → 영속 → 준영속 ↘ 삭제 persist() → 영속 detach() → 준영속 clear() → 영속성 컨텍스트 비우기 remove() → 삭제 merge() → 준영속 Entity를 병합 8. Transaction DB 작업을 하나의 작업 단위로 묶는 것. Transaction 시작 → SQL 작업 → 성공 → COMMIT → 실패 → ROLLBACK COMMIT : 변경사항 확정 ROLLBACK : 변경사항 취소 JPA에서는 Transaction 안에서 Entity를 변경하면 Dirty Checking 을 통해 변경사항을 감지하고 DB에 반영할 수 있다. 9. JPA Auditing Entity의 생성/수정 시간을 자동으로 관리하는 기능. @CreatedDate private LocalDateTime createdAt; @LastModifiedDate private LocalDateTime modifiedAt; 10. Query Method Repository 메서드 이름을 기반으로 쿼리를 생성하는 기능. List<Memo> findByTitle(String title); → title 조건으로 조회하는 쿼리가 생성됨. findByTitle findByTitleAndContents findByTitleContaining 등의 규칙을 사용할 수 있다. 11. Java Generics 클래스나 메서드에서 사용할 타입을 지정하는 기능. List<String> names; JPA Repository에서도 사용된다. JpaRepository<Memo, Long> Memo → 관리할 Entity 타입 Long → Entity의 ID 타입 12. 핵심 흐름 Client ↓ Controller ↓ Service ↓ Repository ↓ Spring Data JPA ↓ JPA / Hibernate ↓ JDBC ↓ Database Spring Container → Bean 관리 → DI → Controller / Service / Repository에 객체 주입 Entity → Persistence Context에서 관리 → Dirty Checking → Transaction Commit → DB 반영 한 줄 회고 오늘은 Spring의 기본 구조부터 JPA의 영속성 컨텍스트와 Transaction까지 전체적인 흐름을 연결해서 이해할 수 있었고, 아직 헷갈리는 JPA 동작 원리는 직접 CRUD를 구현하며 더 익혀봐야겠다.
What RADAR observed and classified to build this opportunity. It is what the source published, not a verification that the offer is still active.
[TIL]. Spring / JPA TIL 1. 오류 해결 Memo 생성자 오류 RequestDto 를 받는 생성자가 없어서 발생. public Memo(RequestDto requestDto) { this.title = requestDto.getTitle(); this.contents = requestDto.getContents(); } IntelliJ: Alt + Insert → Constructor Ctrl + Alt + L → 코드 정렬 Ctrl + Shift + F → 프로젝트 전체 검색 Ctrl + F → 현재 파일 검색 POST 404 오류 Controller와 요청 URL이 달라서 발생. Controller: POST /api/memo 요청: POST /api/memos → API 요청…
Open source