mirror of
https://github.com/Example-uPagge/hibernate-multiple-bag-fetch-exception.git
synced 2024-06-15 10:55:24 +03:00
Fix problem for Spring
This commit is contained in:
parent
f9e1710b2f
commit
17e630f7de
BIN
data/demo.mv.db
BIN
data/demo.mv.db
Binary file not shown.
@ -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<Post> allWithCommentsAndTags = postService.findAllWithCommentsAndTags(1, 50);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
@ -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<Post> allWithCommentsAndTags = postRepository.findAllWithCommentsAndTags(1, 50);
|
||||
final long finish = System.currentTimeMillis();
|
||||
System.out.println(finish - start);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -11,6 +11,7 @@ import javax.persistence.Table;
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(name = "TAG")
|
||||
public class Tag {
|
||||
|
||||
@Id
|
||||
|
@ -10,13 +10,23 @@ import java.util.List;
|
||||
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||
|
||||
@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<Post> findAllWithCommentsAndTags(
|
||||
select distinct p
|
||||
from Post p
|
||||
left join fetch p.comments
|
||||
where p.id between :minId and :maxId
|
||||
""")
|
||||
List<Post> 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<Post> findAllWithTags(
|
||||
@Param("minId") long minId,
|
||||
@Param("maxId") long maxId
|
||||
);
|
||||
|
@ -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<Post> findAllWithCommentsAndTags(long minId, long maxId) {
|
||||
|
||||
final List<Post> posts = postRepository.findAllWithComments(
|
||||
minId,
|
||||
maxId
|
||||
);
|
||||
|
||||
return !posts.isEmpty() ?
|
||||
postRepository.findAllWithTags(
|
||||
minId,
|
||||
maxId
|
||||
) :
|
||||
posts;
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
|
@ -5,8 +5,7 @@
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.hibernate.SQL" level="DEBUG" />
|
||||
<logger name="org.hibernate.type" level="TRACE" />
|
||||
<logger name="org.hibernate.engine.transaction.internal.TransactionImpl" level="DEBUG"/>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
|
Loading…
x
Reference in New Issue
Block a user