From 2ab76e2daba73f3f9ebb2ddc98c11b95172db623 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sat, 2 Jul 2022 18:14:07 +0300 Subject: [PATCH] Fix Variant --- .../hibernate/nbfe/fix/FixProblem.java | 38 +++++++++++++++++++ .../nbfe/{no_fix_set => fix}/domain/Post.java | 10 ++--- .../domain/PostComment.java | 3 +- .../nbfe/{no_fix_set => fix}/domain/Tag.java | 2 +- .../nbfe/no_fix_set/NoFixSetMain.java | 34 ----------------- src/main/resources/META-INF/persistence.xml | 6 +-- 6 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 src/main/java/dev/struchkov/example/hibernate/nbfe/fix/FixProblem.java rename src/main/java/dev/struchkov/example/hibernate/nbfe/{no_fix_set => fix}/domain/Post.java (83%) rename src/main/java/dev/struchkov/example/hibernate/nbfe/{no_fix_set => fix}/domain/PostComment.java (85%) rename src/main/java/dev/struchkov/example/hibernate/nbfe/{no_fix_set => fix}/domain/Tag.java (81%) delete mode 100644 src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/NoFixSetMain.java diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/FixProblem.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/FixProblem.java new file mode 100644 index 0000000..6750387 --- /dev/null +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/FixProblem.java @@ -0,0 +1,38 @@ +package dev.struchkov.example.hibernate.nbfe.fix; + +import dev.struchkov.example.hibernate.nbfe.fix.domain.Post; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; + +import java.util.List; + +public class FixProblem { + + public static void main(String[] args) { + final EntityManagerFactory emf = Persistence.createEntityManagerFactory("Blog"); + final EntityManager entityManager = emf.createEntityManager(); + + final long startTime = System.currentTimeMillis(); + List posts = entityManager.createQuery(""" + select distinct p + from Post p + left join fetch p.comments + where p.id between :minId and :maxId""", Post.class) + .setParameter("minId", 1L) + .setParameter("maxId", 50L) + .getResultList(); + + posts = entityManager.createQuery(""" + select distinct p + from Post p + left join fetch p.tags t + where p in :posts""", Post.class) + .setParameter("posts", posts) + .getResultList(); + + final long finishTime = System.currentTimeMillis(); + System.out.println("Performance: " + (finishTime - startTime)); + } + +} diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/Post.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/Post.java similarity index 83% rename from src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/Post.java rename to src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/Post.java index eeddac6..a165d72 100644 --- a/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/Post.java +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/Post.java @@ -1,4 +1,4 @@ -package dev.struchkov.example.hibernate.nbfe.no_fix_set.domain; +package dev.struchkov.example.hibernate.nbfe.fix.domain; import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; @@ -11,8 +11,8 @@ import jakarta.persistence.OneToMany; import lombok.Getter; import lombok.Setter; -import java.util.HashSet; -import java.util.Set; +import java.util.ArrayList; +import java.util.List; @Entity @Getter @@ -30,7 +30,7 @@ public class Post { cascade = CascadeType.ALL, orphanRemoval = true ) - private Set comments = new HashSet<>(); + private List comments = new ArrayList<>(); @ManyToMany( cascade = { @@ -43,7 +43,7 @@ public class Post { joinColumns = @JoinColumn(name = "post_id"), inverseJoinColumns = @JoinColumn(name = "tag_id") ) - private Set tags = new HashSet<>(); + private List tags = new ArrayList<>(); public void addComment(PostComment postComment) { postComment.setPost(this); diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/PostComment.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/PostComment.java similarity index 85% rename from src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/PostComment.java rename to src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/PostComment.java index d875459..41abda9 100644 --- a/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/PostComment.java +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/PostComment.java @@ -1,4 +1,4 @@ -package dev.struchkov.example.hibernate.nbfe.no_fix_set.domain; +package dev.struchkov.example.hibernate.nbfe.fix.domain; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -22,3 +22,4 @@ public class PostComment { private String review; } + diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/Tag.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/Tag.java similarity index 81% rename from src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/Tag.java rename to src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/Tag.java index 9f8555b..d86837f 100644 --- a/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/domain/Tag.java +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/fix/domain/Tag.java @@ -1,4 +1,4 @@ -package dev.struchkov.example.hibernate.nbfe.no_fix_set.domain; +package dev.struchkov.example.hibernate.nbfe.fix.domain; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/NoFixSetMain.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/NoFixSetMain.java deleted file mode 100644 index ac230b4..0000000 --- a/src/main/java/dev/struchkov/example/hibernate/nbfe/no_fix_set/NoFixSetMain.java +++ /dev/null @@ -1,34 +0,0 @@ -package dev.struchkov.example.hibernate.nbfe.no_fix_set; - -import dev.struchkov.example.hibernate.nbfe.no_fix_set.domain.Post; -import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.Persistence; - -import java.util.List; - -public class NoFixSetMain { - - public static void main(String[] args) { - final EntityManagerFactory emf = Persistence.createEntityManagerFactory("Blog"); - final EntityManager entityManager = emf.createEntityManager(); - - final long startTime = System.currentTimeMillis(); - final List posts = entityManager.createQuery( - """ - SELECT p - FROM Post p - LEFT JOIN FETCH p.comments - LEFT JOIN FETCH p.tags - WHERE p.id BETWEEN :minId AND :maxId - """, Post.class - ) - .setParameter("minId", 1L) - .setParameter("maxId", 50L) - .getResultList(); - - final long finishTime = System.currentTimeMillis(); - System.out.println("Performance: " + (startTime - finishTime)); - } - -} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index 70b42b9..71147ad 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -4,9 +4,9 @@ http://xmlns.jcp.org/xml/ns/persistence_2_1.xsd"> - dev.struchkov.example.hibernate.nbfe.no_fix_set.domain.Post - dev.struchkov.example.hibernate.nbfe.no_fix_set.domain.PostComment - dev.struchkov.example.hibernate.nbfe.no_fix_set.domain.Tag + dev.struchkov.example.hibernate.nbfe.fix.domain.Post + dev.struchkov.example.hibernate.nbfe.fix.domain.PostComment + dev.struchkov.example.hibernate.nbfe.fix.domain.Tag true