작성
·
633
0
@Test
void contextLoads() {
JPAQueryFactory query = new JPAQueryFactory(em);
QHello qHello = QHello.hello; //Querydsl Q타입 동작 확인
List<Hello> result = query
.selectFrom(qHello).fetch();
System.out.println("result = " + result);
}
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PostPersist;
import javax.persistence.PreUpdate;
import javax.persistence.PostUpdate;
import javax.persistence.PreRemove;
import javax.persistence.PostRemove;
// import org.springframework.beans.BeanUtils;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Builder;
@Entity
@Table(name="EDU_ACNT") //--PRIMARY
@Getter
@NoArgsConstructor // (access = AccessLevel.PROTECTED) // AccessLevel.PUBLIC
@EqualsAndHashCode // (of = {"email","name"}, callSuper = true, onParam = @__(@NonNull))
@ToString //(exclude = "password") // exclude 속성을 사용하면, 특정 필드를 toString() 결과에서 제외
public class EduAcnt {
@Id // Integer
@Column(name = "ACNT_NO", nullable = false)
private Integer acntNo; //--null
@Column(name = "BALANCE", precision = 18, scale = 0, nullable = false)
private BigDecimal balance; //--null
@Builder
public EduAcnt(Integer acntNo, BigDecimal balance) {
this.acntNo = acntNo;
this.balance = balance;
}
//----------------------------------------------------
// Load/Persist/Update/Remove(조회/신규/수정/삭제)
// Entity Pre/Post(이전/이후) 처리에 대한 정의(PreLoad는 없음)
// * DB의 Trigger와 비슷한 JPA기능
//----------------------------------------------------
@PostLoad
public void onPostLoad() {
}
@PrePersist
public void onPrePersist() {
}
@PostPersist
public void onPostPersist() {
}
@PreUpdate
public void onPreUpdate() {
}
@PostUpdate
public void onPostUpdate() {
}
@PreRemove
public void onPreRemove() {
}
@PostRemove
public void onPostRemove() {
}
}
답변 1
0
안녕하세요. 김기정님
Querydsl 설정이 버전이나 환경에 따라서 조금씩 달라집니다.
다음을 참고해주세요.
plugins {
id 'org.springframework.boot' version '2.6.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
ext["hibernate.version"] = "5.6.5.Final"
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
//JdbcTemplate 추가
//implementation 'org.springframework.boot:spring-boot-starter-jdbc'
//MyBatis 추가
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
//JPA, 스프링 데이터 JPA 추가
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//Querydsl 추가
implementation 'com.querydsl:querydsl-jpa'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
//H2 데이터베이스 추가
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//테스트에서 lombok 사용
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
}
tasks.named('test') {
useJUnitPlatform()
}
//Querydsl 추가, 자동 생성된 Q클래스 gradle clean으로 제거
clean {
delete file('src/main/generated')
}