본문 바로가기

Back-End/JPA

(17)
Spring jpa save(), saveAndFlush() 제대로 알고 쓰기 @Transactional 없이 save() vs saveAndFlush() public Member saveMember(MemberDTO memberDTO){ Member member = Member.create(memberDTO); member = memberRepository.save(member); member.setName("TaeHyun"); return member;// break point 설정 } 아래와 같이 console에 출력되지만 변경된 name(태현) 은 입력되지 않는다. Hibernate : insert into member (name, uuid ... ) values (?,?, ... ) public Member saveMember(MemberDTO memberDTO){ Mem..
ObjectOptimisticLockingFailureException 11/1/2019 5:47:27 PMorg.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 11/1/2019 5:47:27 PMat org.springframework.orm.jpa.vendor.HibernateJpaD..
N+1 문제 해결하기 ( fetch join, @EntityGraph ) 댓글과 대댓글을 조회하는 API를 개발하였는데, 검색이 비이상적으로 오래 걸려 Query문을 다시 봤다. 1. 기존 Query @Query(value = "SELECT c FROM Comment c WHERE c.goodsId = ?1 AND c.id = c.parentIdx " + "AND c.memberName not in ?2 ORDER BY c.regDate DESC, c.commentRate DESC ") Page findCommentsByGoodsIdExcludeAdminComment(Long goodsId, Collection excludeMembers, Pageable pageable); Hibernate: select comment0_.good_comment_idx as good_com1..
JPA 1차 캐시 - Database와 동기화가 되지 않은 데이터를 읽는 문제 문제 Data Data` Data Service 1 => => Data 수정 Service 2 => Data 조회 ( 캐시에 남음 ) => Data 조회하려 했지만 수정된 데이터가 아니라 캐시에 남은 Data를 조회함 수정된 Data가 검색되는게 아니라 JPA 1차캐시에 남아 있는 기존 Data를 Service 2에서 검색하는 문제였다. 스프링 마이크로서비스를 개발하면서 이런 일이 많을 것 같은데 어떠한 해결방법이 있을까? 해결방법 1. flush() Service2에서 Data를 조회하기 전에 flush()를 호출하여 Database와 1차 캐시를 동기화해주는 방법 하지만 성능상 이슈?
13장. 웹애플리케이션과 영속성 관리 JPA를 이용하여 Spring 개발을 하다보면, LazyInitializationException 같은 예외를 겪어 봤을 것이다. 나도 JPA의 내부동작방식을 이해하지 못하고 있었기에 어떤 문제인지 파악을 하지 못하였다. 그래서 13장. 웹 어플리케이션과 영속성 관리를 읽게 되었다. 트랜잭션 범위의 영속성 컨텍스트 ( persistence context in transaction scope ) 스프링 컨테이너의 기본 전략 스프링 컨테이너는 트랜잭션 범위의 영속성 컨텍스트 전략을 기본으로 사용 한다. 즉, 트랜잭션이 시작될 때 영속성 컨텍스트를 생성하고 트랜잭션이 끝날 때 영속성 컨텍스트를 종료한다. 그리고 같은 트랜잭션 안에서는 항상 같은 영속성 컨텍스트에 접근한다. @Transactional 어노테이션..
Persistence Framework에 관하여 ( 장,단점 ) ORM이란?객체 데이터베이스 데이터 자동 매핑 ( Object Relational Mapping ) Persistence란?데이터를 생성한 프로그램이 종료되더라도 사라지지 않는 데이터 Persistence Framework?데이터베이스와의 연동되는 시스템을 빠르게 개발하고 안정적인 구동을 보장해주는 프레임워크 종류SQL Mapper : SQL문장으로 직접 데이터베이스 데이터를 다루는 SQL Helper ( Mybatis )ORM : 객체를 통해 간접적으로 데이터베이스를 다루는 ORM ( Hibernate, JPA ) Persistence Framework를 사용하면 좋은 점DBMS에 대한 종속성이 줄어든다.DBMS 교체작업 때 해야할 일이 줄어든다.자바 객체에 매핑되어 있기 때문에 자바의 기능 ( equ..
JPA 3장. 영속성 관리 ( persistence ) JPA에서 제공해주는 기능은 크게 2가지이다.@Entity와 Table을 매핑하는 설계 부분매핑한 @Entity를 실제로 사용하는 부분 ( CRUD ) Spring Boot 프로젝트를 할 때 @Entity 어노테이션을 명시만 해봤는데, 이번 포스팅에서 EntityManager가 매핑한 Entity를 어떻게 사용하는지 살펴보자. Entity Manager와 Entity Manager FactoryEntity Manager Factory데이터베이스를 하나만 사용하는 애플리케이션에서는 하나의 Entity Manager Factory를 생성한다. 설정 정보는 META-INF/persistence.xml에서 읽어 온다.EntityManagerFactory emf = Persistence.createEntityMa..