diff --git a/data/demo.mv.db b/data/demo.mv.db index 7ee7076..708ffdf 100644 Binary files a/data/demo.mv.db and b/data/demo.mv.db differ diff --git a/src/main/java/dev/struchkov/example/spring/nbfe/ListenerForRun.java b/src/main/java/dev/struchkov/example/spring/nbfe/ListenerForRun.java new file mode 100644 index 0000000..707726e --- /dev/null +++ b/src/main/java/dev/struchkov/example/spring/nbfe/ListenerForRun.java @@ -0,0 +1,24 @@ +package dev.struchkov.example.spring.nbfe; + +import dev.struchkov.example.spring.nbfe.domain.Post; +import dev.struchkov.example.spring.nbfe.service.PostService; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class ListenerForRun implements ApplicationRunner { + + private final PostService postService; + + @Override + public void run(ApplicationArguments args) throws Exception { + final List allWithCommentsAndTags = postService.findAllWithCommentsAndTags(1, 50); + System.out.println(); + } + +} diff --git a/src/main/java/dev/struchkov/example/spring/nbfe/SpringApp.java b/src/main/java/dev/struchkov/example/spring/nbfe/SpringApp.java index fe41f58..88cecd1 100644 --- a/src/main/java/dev/struchkov/example/spring/nbfe/SpringApp.java +++ b/src/main/java/dev/struchkov/example/spring/nbfe/SpringApp.java @@ -1,31 +1,15 @@ package dev.struchkov.example.spring.nbfe; -import dev.struchkov.example.spring.nbfe.domain.Post; -import dev.struchkov.example.spring.nbfe.repository.PostRepository; -import lombok.RequiredArgsConstructor; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.event.ContextStartedEvent; -import org.springframework.context.event.EventListener; - -import java.util.List; +import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication -@RequiredArgsConstructor +@EnableTransactionManagement public class SpringApp { - private final PostRepository postRepository; - public static void main(String[] args) { SpringApplication.run(SpringApp.class, args); } - @EventListener - public void run(ContextStartedEvent event) { - final long start = System.currentTimeMillis(); - final List allWithCommentsAndTags = postRepository.findAllWithCommentsAndTags(1, 50); - final long finish = System.currentTimeMillis(); - System.out.println(finish - start); - } - } diff --git a/src/main/java/dev/struchkov/example/spring/nbfe/domain/Post.java b/src/main/java/dev/struchkov/example/spring/nbfe/domain/Post.java index 304d8b5..bf76b47 100644 --- a/src/main/java/dev/struchkov/example/spring/nbfe/domain/Post.java +++ b/src/main/java/dev/struchkov/example/spring/nbfe/domain/Post.java @@ -11,12 +11,14 @@ import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; +import javax.persistence.Table; import java.util.ArrayList; import java.util.List; @Entity @Getter @Setter +@Table(name = "POST") public class Post { @Id diff --git a/src/main/java/dev/struchkov/example/spring/nbfe/domain/PostComment.java b/src/main/java/dev/struchkov/example/spring/nbfe/domain/PostComment.java index 0325bef..6b80a14 100644 --- a/src/main/java/dev/struchkov/example/spring/nbfe/domain/PostComment.java +++ b/src/main/java/dev/struchkov/example/spring/nbfe/domain/PostComment.java @@ -7,10 +7,12 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; +import javax.persistence.Table; @Entity @Getter @Setter +@Table(name = "POSTCOMMENT") public class PostComment { @Id diff --git a/src/main/java/dev/struchkov/example/spring/nbfe/domain/Tag.java b/src/main/java/dev/struchkov/example/spring/nbfe/domain/Tag.java index b31ee5b..fd4c7a2 100644 --- a/src/main/java/dev/struchkov/example/spring/nbfe/domain/Tag.java +++ b/src/main/java/dev/struchkov/example/spring/nbfe/domain/Tag.java @@ -11,6 +11,7 @@ import javax.persistence.Table; @Entity @Getter @Setter +@Table(name = "TAG") public class Tag { @Id diff --git a/src/main/java/dev/struchkov/example/spring/nbfe/repository/PostRepository.java b/src/main/java/dev/struchkov/example/spring/nbfe/repository/PostRepository.java index 0c354b3..e23a8a2 100644 --- a/src/main/java/dev/struchkov/example/spring/nbfe/repository/PostRepository.java +++ b/src/main/java/dev/struchkov/example/spring/nbfe/repository/PostRepository.java @@ -10,13 +10,23 @@ import java.util.List; public interface PostRepository extends JpaRepository { @Query(""" - select distinct p - from Post p - left join fetch p.comments - left join fetch p.tags - where p.id between :minId and :maxId - """) - List findAllWithCommentsAndTags( + select distinct p + from Post p + left join fetch p.comments + where p.id between :minId and :maxId + """) + List findAllWithComments( + @Param("minId") long minId, + @Param("maxId") long maxId + ); + + @Query(""" + select distinct p + from Post p + left join fetch p.tags + where p.id between :minId and :maxId + """) + List findAllWithTags( @Param("minId") long minId, @Param("maxId") long maxId ); diff --git a/src/main/java/dev/struchkov/example/spring/nbfe/service/PostService.java b/src/main/java/dev/struchkov/example/spring/nbfe/service/PostService.java new file mode 100644 index 0000000..7f76166 --- /dev/null +++ b/src/main/java/dev/struchkov/example/spring/nbfe/service/PostService.java @@ -0,0 +1,33 @@ +package dev.struchkov.example.spring.nbfe.service; + +import dev.struchkov.example.spring.nbfe.domain.Post; +import dev.struchkov.example.spring.nbfe.repository.PostRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class PostService { + + private final PostRepository postRepository; + + @Transactional(readOnly = true) + public List findAllWithCommentsAndTags(long minId, long maxId) { + + final List posts = postRepository.findAllWithComments( + minId, + maxId + ); + + return !posts.isEmpty() ? + postRepository.findAllWithTags( + minId, + maxId + ) : + posts; + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 010f693..7be281e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,14 @@ spring: + datasource: + driver-class-name: org.h2.Driver + url: jdbc:h2:file:./data/demo + username: sa + password: jpa: show-sql: true + database-platform: org.hibernate.dialect.H2Dialect properties: hibernate: format_sql: true + hibernate: + ddl-auto: none diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 86db7c9..66d5117 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -5,8 +5,7 @@ - - +