Fix Variant

This commit is contained in:
Struchkov Mark 2022-07-02 18:14:07 +03:00
parent 872cc06c8b
commit 2ab76e2dab
6 changed files with 49 additions and 44 deletions

View File

@ -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<Post> 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));
}
}

View File

@ -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<PostComment> comments = new HashSet<>();
private List<PostComment> comments = new ArrayList<>();
@ManyToMany(
cascade = {
@ -43,7 +43,7 @@ public class Post {
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id")
)
private Set<Tag> tags = new HashSet<>();
private List<Tag> tags = new ArrayList<>();
public void addComment(PostComment postComment) {
postComment.setPost(this);

View File

@ -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;
}

View File

@ -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;

View File

@ -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<Post> 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));
}
}

View File

@ -4,9 +4,9 @@
http://xmlns.jcp.org/xml/ns/persistence_2_1.xsd">
<persistence-unit name="Blog">
<class>dev.struchkov.example.hibernate.nbfe.no_fix_set.domain.Post</class>
<class>dev.struchkov.example.hibernate.nbfe.no_fix_set.domain.PostComment</class>
<class>dev.struchkov.example.hibernate.nbfe.no_fix_set.domain.Tag</class>
<class>dev.struchkov.example.hibernate.nbfe.fix.domain.Post</class>
<class>dev.struchkov.example.hibernate.nbfe.fix.domain.PostComment</class>
<class>dev.struchkov.example.hibernate.nbfe.fix.domain.Tag</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>