반응형
1교시 #캐시
- JPA 1차 캐시와 2차 캐시 소개
-JPA 영속성 컨텍스트란?
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;
}
@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);
}
@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> findByMyBook(String title);
List<Book> bookList = bookRepository.findByMyBook("자바책");
bookList.forEach(System.out::println);
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>> findByNamedTitleIdByMyBooks(@Param("title") String title);
List<Map<String, Object>> listMap2 = bookRepository.findByNamedTitleIdByMyBooks("자바책");
listMap2.forEach(x -> System.out.println(x.entrySet()));
System.out.println("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();
}
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> findByCustomNamedTitleIdByMyBooks(@Param("title") String title);
List<BookAndId> bookAndIdList = bookRepository.findByCustomNamedTitleIdByMyBooks("자바책");
bookAndIdList.forEach(s -> System.out.println(s.getAbc() + " : " + s.getTitle2()));
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> findByCustomNamedNameIdByMyBooks(@Param("title") String title);
List<Tuple> bookAndIdList = bookRepository.findByCustomNamedNameIdByMyBooks("자바책");
bookAndIdList.forEach(tuple -> System.out.println(tuple.get(0) + " : " + tuple.get(1)));
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);
@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);
@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);
}
void updateTest1() {
int isUpdate = bookRepository.updateSpecificName(2L);
System.out.println("2번 id를 book의 이름 " + (isUpdate > 0? "바뀜" : "안바뀜") + isUpdate);
}
2번 id를 book의 이름 바뀜1
반응형
'2023 학원 수업 일지' 카테고리의 다른 글
0719 수업 (0) | 2023.07.19 |
---|---|
수업 33일차 - html/css/javascript 11 (0) | 2023.04.19 |
수업 32일차 - html/css/javascript 10 (0) | 2023.04.18 |
수업 31일차 - html/css/javascript 9 (1) | 2023.04.18 |
수업 30일차 - html/css/javascript 8 (0) | 2023.04.14 |