Skip to content

Commit

Permalink
Merge pull request #79 from onetime-with-members/feature/#78/query-dsl
Browse files Browse the repository at this point in the history
[feat] : 쿼리 DSL 기능을 추가한다
  • Loading branch information
bbbang105 authored Oct 14, 2024
2 parents 38bb02d + 7b40a4d commit e6b6ff1
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 9 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ dependencies {
// DB
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
// Query DSL
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
// Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
// Security
Expand All @@ -49,3 +54,5 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}

def querydslDir = "src/main/generated"
3 changes: 0 additions & 3 deletions src/main/java/side/onetime/domain/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ public class Event extends BaseEntity {
private Category category;

@OneToMany(mappedBy = "event",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<Member> members;

@OneToMany(mappedBy = "event",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<Schedule> schedules;

@OneToMany(mappedBy = "event",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<EventParticipation> eventParticipations;

@Builder
Expand Down
1 change: 0 additions & 1 deletion src/main/java/side/onetime/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class Member extends BaseEntity {
private String pin;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<Selection> selections;

@Builder
Expand Down
1 change: 0 additions & 1 deletion src/main/java/side/onetime/domain/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class Schedule extends BaseEntity {
private String time;

@OneToMany(mappedBy = "schedule",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<Selection> selections;

@Builder
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/side/onetime/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ public class User extends BaseEntity {
private String providerId;

@OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<Selection> selections;

@OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchSize(size = 100)
private List<EventParticipation> eventParticipations;

@Builder
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/side/onetime/global/config/QueryDslConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package side.onetime.global.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueryDslConfig {
@PersistenceContext
private EntityManager entityManager;

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}
3 changes: 2 additions & 1 deletion src/main/java/side/onetime/repository/EventRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import org.springframework.data.jpa.repository.JpaRepository;
import side.onetime.domain.Event;
import side.onetime.repository.custom.EventRepositoryCustom;

import java.util.Optional;
import java.util.UUID;

public interface EventRepository extends JpaRepository<Event,Long> {
public interface EventRepository extends JpaRepository<Event,Long>, EventRepositoryCustom {
Optional<Event> findByEventId(UUID eventId);
boolean existsByEventId(UUID eventId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package side.onetime.repository.custom;

import side.onetime.domain.Event;

public interface EventRepositoryCustom {
void deleteUserEvent(Event event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package side.onetime.repository.custom;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import side.onetime.domain.Event;

import static side.onetime.domain.QEvent.event;
import static side.onetime.domain.QEventParticipation.eventParticipation;
import static side.onetime.domain.QMember.member;
import static side.onetime.domain.QSchedule.schedule;
import static side.onetime.domain.QSelection.selection;

@RequiredArgsConstructor
public class EventRepositoryImpl implements EventRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public void deleteUserEvent(Event e) {
queryFactory.delete(selection)
.where(selection.schedule.event.eq(e))
.execute();

queryFactory.delete(eventParticipation)
.where(eventParticipation.event.eq(e))
.execute();

queryFactory.delete(schedule)
.where(schedule.event.eq(e))
.execute();

queryFactory.delete(member)
.where(member.event.eq(e))
.execute();

queryFactory.delete(event)
.where(event.eq(e))
.execute();
}
}
2 changes: 1 addition & 1 deletion src/main/java/side/onetime/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,6 @@ public void removeUserCreatedEvent(String authorizationHeader, String eventId) {
throw new EventParticipationException(EventParticipationErrorResult._IS_NOT_USERS_CREATED_EVENT_PARTICIPATION);
}

eventRepository.delete(event);
eventRepository.deleteUserEvent(event);
}
}

0 comments on commit e6b6ff1

Please sign in to comment.