2023 학원 수업 일지

0717 수업

웨일파도 2023. 7. 17. 17:09
반응형

1교시 #캐시 

- JPA 1차 캐시와 2차 캐시 소개

https://devbksheen.tistory.com/entry/JPA-1%EC%B0%A8-%EC%BA%90%EC%8B%9C%EC%99%80-2%EC%B0%A8-%EC%BA%90%EC%8B%9C-%EC%86%8C%EA%B0%9C

-JPA 영속성 컨텍스트란?

https://velog.io/@neptunes032/JPA-%EC%98%81%EC%86%8D%EC%84%B1-%EC%BB%A8%ED%85%8D%EC%8A%A4%ED%8A%B8%EB%9E%80

 

2교시

@트랜잭션

 

3교시 

쓰기 지연(p.113)

 

@DynamicUpdate

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@DynamicUpdate
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
}
@Test
@Transactional
@Commit
void persisCacheDelayUpdateTest() {
Users user1 = usersRepository.findById(1L).get();
user1.setName("최은아");
usersRepository.save(user1);
}

(@DynamicUpdate 있을 때)  => 효율적

update users set name='최은아' where id=1;

(@DynamicUpdate 없을 때)  => '최은아' name만 update하면 되는데 굳이 기존에 있는 email 도 set 함

update users set email='kim0mi@naver.com',name='최은아' where id=1;

 

 

 

일대다 - LAZY(지연로딩) 가 디폴트 @OneToMany(fetch = FetchType.LAZY)

일대일 -  EAGER(즉시로딩) 가 디폴트 @OneToOne(fetch = FetchType.EAGER)

 

4교시

엔티티의 상태

https://brightstarit.tistory.com/25

 

[JPA] flush(), detach(), clear(), close(), merge()

플러시 플러시(flush())는 영속성 컨텍스트의 변경 내용을 데이터베이스에 반영한다. 영속성 컨텍스트를 플러시 하는 방법은 3가지다. em.flush()를 직접 호출한다. 엔티티 매니저의 flush() 메소드를

brightstarit.tistory.com

 

5교시 - 8교시

JPA - JPQL : 엔티티 기반 / Java로 된 SQL

@Query(value = "select b from Book b where title = ?1")
List<Book> findByMyBook(String title);
List<Book> bookList = bookRepository.findByMyBook("자바책");
bookList.forEach(System.out::println);

 

@Query(value = "select b.id id, b.title title from Book b where title = :title")
List<Map<String, Object>> findByNamedTitleIdByMyBooks(@Param("title") String title);
List<Map<String, Object>> listMap2 = bookRepository.findByNamedTitleIdByMyBooks("자바책");
listMap2.forEach(x -> System.out.println(x.entrySet()));
System.out.println("findByNamedTitleIdByMyBooks--------------------------------------------------------------------");
select b1_0.id,b1_0.title from book b1_0 where b1_0.title='자바책';

[title=자바책, id=1]
[title=자바책, id=4]

 

public interface BookAndId {
Long getAbc();
String getTitle2();
}
@Query(value = "select b.id abc, b.title title2 from Book b where title = :title")
List<BookAndId> findByCustomNamedTitleIdByMyBooks(@Param("title") String title);
List<BookAndId> bookAndIdList = bookRepository.findByCustomNamedTitleIdByMyBooks("자바책");
bookAndIdList.forEach(s -> System.out.println(s.getAbc() + " : " + s.getTitle2()));
select b1_0.id,b1_0.title from book b1_0 where b1_0.title='자바책';

1 : 자바책
4 : 자바책
import jakarta.persistence.Tuple;
@Query(value = "select b.id abc, b.title title2 from Book b where title = :title")
List<Tuple> findByCustomNamedNameIdByMyBooks(@Param("title") String title);
List<Tuple> bookAndIdList = bookRepository.findByCustomNamedNameIdByMyBooks("자바책");
bookAndIdList.forEach(tuple -> System.out.println(tuple.get(0) + " : " + tuple.get(1)));
select b1_0.id,b1_0.title from book b1_0 where b1_0.title='자바책';
1 : 자바책
4 : 자바책

 

@Transactional
@Query(value = "update book set title = '이상한자바책' where id = :id", nativeQuery = true)
@Modifying
int updateSpecificName(@Param("id") Long id);
@Transactional
@Modifying
@Query(value = """
     update
        Book b
     set
        b.name = '이상한자바책'
    where
       b.id = :id
     """)
int updateSpecificNameByJPQL(@Param("id") Long id);
@Test
void updateTest1() {
int isUpdate = bookRepository.updateSpecificName(2L);
System.out.println("2 id book의 이름 " + (isUpdate > 0? "바뀜" : "안바뀜") + isUpdate);
}

2번 id를 book의 이름 바뀜1

반응형