QueryDSL build.gradle 설정
buildscript {
ext {
set('querydsl.version', '5.0.0') // 상위버전
}
}
plugins {
id 'org.springframework.boot' version '2.5.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
dependencies {
implementation 'com.querydsl:querydsl-jpa'
implementation 'com.querydsl:querydsl-core'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
annotationProcessor 'jakarta.persistence:jakarta.persistence-api'
annotationProcessor 'jakarta.annotation:jakarta.annotation-api'
}
// QueryDSL
def querydslDir = "$buildDir/generated" as Object
task cleanQueryDsl {
delete file(querydslDir)
}
QueryDSL 4.0.x list() 메소드 삭제
QueryDSL 4.0.x ~ 버전부터는 list()메소드가 삭제되었다.
https://stackoverflow.com/questions/33102742/mysema-querydsl-theres-no-jpaquerylist-method
대신에, fetch() 메소드를 사용할 수 있다. 그리고 JPAQuery대신에, JPAQueryFactory 클래스를 사용한다.
https://joont92.github.io/jpa/QueryDSL/
https://querydsl.com/static/querydsl/4.4.0/reference/html/ch02.html
= QueryDSL 4.4.0 기준 예제코드
public class ProductRepository {
@PersistenceContext
private final EntityManager entityManager;
public ProductRepository(EntityManager entityManager) {
this.entityManager = entityManager;
}
// 상품목록
public List<Product> getProducts() {
JPAQueryFactory query = new JPAQueryFactory(this.entityManager);
QProduct qProduct = new QProductEntity("product");
return query.selectFrom(qProduct) // select()[전체선택] + from()
.where(qProduct.storeEntity.storeId.eq(1))
.orderBy(qProduct.productId.desc())
.offset(0)
.limit(20)
.fetch(); // list() 메소드를 대신해서 사용한다.
}
// 상품등록
@Transactional
public boolean setProduct(Long storeId, ProductRequestDto productRequestDto) {
EntityManager entityManager = this.entityManager;
// store_id만 등록해야하는데, Entity를 Mapping할 수 없다.
String sql = "" +
"INSERT INTO" +
" `ys-lab`.`product`" +
" (store_id, `name`, name_sub, origin_price, price, description, is_sale)" +
"VALUES" +
" (?, ?, ?, ?, ?, ?, ?)";
Query query = entityManager.createNativeQuery(sql);
query.setParameter(1, storeId);
query.setParameter(2, productRequestDto.getName());
query.setParameter(3, productRequestDto.getNameSub());
query.setParameter(4, productRequestDto.getOriginPrice());
query.setParameter(5, productRequestDto.getPrice());
query.setParameter(6, productRequestDto.getDescription());
query.setParameter(7, productRequestDto.getIsSale());
return (query.executeUpdate() == 1);
}
// 상품 수정
@Transactional
public boolean updateProduct(Long productId, ProductRequestDto productRequestDto) {
JPAQueryFactory query = new JPAQueryFactory(this.entityManager);
QProductEntity qProductEntity = new QProductEntity("updateProduct");
long result = query.update(qProductEntity)
.set(qProductEntity.name, productRequestDto.getName())
.set(qProductEntity.nameSub, productRequestDto.getNameSub())
.set(qProductEntity.originPrice, productRequestDto.getOriginPrice())
.set(qProductEntity.price, productRequestDto.getPrice())
.set(qProductEntity.description, productRequestDto.getDescription())
.set(qProductEntity.isSale, productRequestDto.getIsSale().intValue())
.set(qProductEntity.updateDate, LocalDateTime.now())
.where(qProductEntity.productId.eq(productId))
.execute();
return (result == 1);
}
// 상품 삭제
@Transactional
public boolean deleteProduct(Long productId) {
JPAQueryFactory query = new JPAQueryFactory(this.entityManager);
QProductEntity qProductEntity = new QProductEntity("updateProduct");
long result = query.delete(qProductEntity)
.where(qProductEntity.productId.eq(productId))
.execute();
return (result == 1);
}
}
'SpringFramework > JPA' 카테고리의 다른 글
JPA - 트랜잭션과 락 (+격리수준) (0) | 2021.11.29 |
---|---|
JPA - N+1 문제 (0) | 2021.11.29 |
JPA - 스프링 데이터 JPA (0) | 2021.11.23 |
JPA - QueryDSL (0) | 2021.11.19 |
JPA - Criteria (0) | 2021.11.19 |