๐ค์์ ๋ฐฐ์ ๋ ๋ด์ฉ๋ง ํ์ฉํ ์์์ผ๋ฉด JPA๋ ์ธ์ ์๋ค. ํ์ง๋ง ์ฝ๊ฐ์ ์์ธ์ ์ธ ์ํฉ์์ Customํด์ผํ๋ ์ํฉ์ด ์๊ธด๋ค. ๊ทธ๋ด๋๋ @Query๋ฅผ ์จ๋ณด์.
// ์นดํ
๊ณ ๋ฆฌ๊ฐ null์ด๋ฉด์ ์ด๋ฆ์ ์ง์ ํ๊ฐ์ผ๋ก,createdAt ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , updatedAt ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ์ฟผ๋ฆฌ
List<Book> findByCategoryIsNullAndNameEqualsAndCreatedAtGreaterThanEqualAndUpdatedAtGreaterThanEqual(String name, LocalDateTime createdAt,LocalDateTime updatedAt);
@Test
void queryTest() {
bookRepository.findAll().forEach(System.out::println);
System.out.println("findByCategoryIsNullAndNameEqualsAndCreatedAtGreaterThanEqualAndUpdatedAtGreaterThanEqual : " +
bookRepository.findByCategoryIsNullAndNameEqualsAndCreatedAtGreaterThanEqualAndUpdatedAtGreaterThanEqual(
"JPA ์์๊ฒฉ์ฐจ ํด๋์ค",
LocalDateTime.now().minusDays(1L),
LocalDateTime.now().minusDays(1L)
));
insert into publisher(`id`,`name`,`created_At`,`updated_At`) value (1, '๋น ๋ฅธ๋ํ',now(),now());
insert into book(`id`,`name`,`publisher_id`, `deleted`, `created_At`,`updated_At`) values (2,'Spring Security ์์๊ฒฉ์ฐจ ํด๋์ค', 1,false,now(),now());
insert into book(`id`,`name`,`publisher_id`, `deleted`, `created_At`,`updated_At`) values (3,'SpringBoot ํ๋์ธ ํด๋์ค', 1,true,now(),now());
insert into book(`id`,`name`,`publisher_id`, `deleted`, `created_At`,`updated_At`) values (1,'JPA ์์๊ฒฉ์ฐจ ํด๋์ค', 1,false,now(),now());
@EntityListeners(value = AuditingEntityListener.class)
public class BaseEntity implements Auditable {
@CreatedDate
@Column(columnDefinition = "datetime(6) default now(6) comment '์์ฑ์๊ฐ'", nullable = false, updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
@Column(columnDefinition = "datetime(6) default now(6) comment '์์ ์๊ฐ'", nullable = false)
private LocalDateTime updatedAt;
}
create table author (
id bigint not null auto_increment,
created_at datetime(6) default now(6) comment '์์ฑ์๊ฐ' not null,
updated_at datetime(6) default now(6) comment '์์ ์๊ฐ' not null,
@Query(value = "select b from Book b where name = ?1 and createdAt >= ?2 and updatedAt >= ?3 and category is null")
List<Book> findByNameRecently(String name, LocalDateTime createdAt,LocalDateTime updatedAt);
@Test
void queryTest() {
System.out.println("findByNameRecently : " +
bookRepository.findByNameRecently(
"JPA ์์๊ฒฉ์ฐจ ํด๋์ค",
LocalDateTime.now().minusDays(1L),
LocalDateTime.now().minusDays(1L)));
@Query(value = "select b from Book b where name = :name and createdAt >= :createdAt and updatedAt >= :updatedAt and category is null")
List<Book> findByNameRecently(
@Param("name") String name,
@Param("createdAt") LocalDateTime createdAt,
@Param("updatedAt") LocalDateTime updatedAt);
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookNameAndCategory {
private String name;
private String category;
}
@Query(value = "select new com.practice.jpa.bookmanager.repository.dto.BookNameAndCategory(b.name, b.category) from Book b")
List<BookNameAndCategory> findBookNameAndCategory();
@Query(value = "select new com.practice.jpa.bookmanager.repository.dto.BookNameAndCategory(b.name, b.category) from Book b")
Page<BookNameAndCategory> findBookNameAndCategory(Pageable pageable);
test
bookRepository.findBookNameAndCategory(PageRequest.of(1, 1)).forEach(
bookNameAndCategory -> System.out.println(bookNameAndCategory.getName() + " : " + bookNameAndCategory.getCategory()));
bookRepository.findBookNameAndCategory(PageRequest.of(0, 1)).forEach(
bookNameAndCategory -> System.out.println(bookNameAndCategory.getName() + " : " + bookNameAndCategory.getCategory()));