No Fix Variant

This commit is contained in:
Struchkov Mark 2022-07-02 18:09:14 +03:00
parent 57b0983003
commit 872cc06c8b
5 changed files with 17 additions and 14 deletions

View File

@ -1,18 +1,19 @@
package dev.struchkov.example.hibernate.nbfe.problem; package dev.struchkov.example.hibernate.nbfe.no_fix_set;
import dev.struchkov.example.hibernate.nbfe.problem.domain.Post; import dev.struchkov.example.hibernate.nbfe.no_fix_set.domain.Post;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence; import jakarta.persistence.Persistence;
import java.util.List; import java.util.List;
public class ProblemMain { public class NoFixSetMain {
public static void main(String[] args) { public static void main(String[] args) {
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("Blog"); final EntityManagerFactory emf = Persistence.createEntityManagerFactory("Blog");
final EntityManager entityManager = emf.createEntityManager(); final EntityManager entityManager = emf.createEntityManager();
final long startTime = System.currentTimeMillis();
final List<Post> posts = entityManager.createQuery( final List<Post> posts = entityManager.createQuery(
""" """
SELECT p SELECT p
@ -25,7 +26,9 @@ public class ProblemMain {
.setParameter("minId", 1L) .setParameter("minId", 1L)
.setParameter("maxId", 50L) .setParameter("maxId", 50L)
.getResultList(); .getResultList();
final long finishTime = System.currentTimeMillis();
System.out.println("Performance: " + (startTime - finishTime));
} }
} }

View File

@ -1,4 +1,4 @@
package dev.struchkov.example.hibernate.nbfe.problem.domain; package dev.struchkov.example.hibernate.nbfe.no_fix_set.domain;
import jakarta.persistence.CascadeType; import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
@ -11,8 +11,8 @@ import jakarta.persistence.OneToMany;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.ArrayList; import java.util.HashSet;
import java.util.List; import java.util.Set;
@Entity @Entity
@Getter @Getter
@ -30,7 +30,7 @@ public class Post {
cascade = CascadeType.ALL, cascade = CascadeType.ALL,
orphanRemoval = true orphanRemoval = true
) )
private List<PostComment> comments = new ArrayList<>(); private Set<PostComment> comments = new HashSet<>();
@ManyToMany( @ManyToMany(
cascade = { cascade = {
@ -43,7 +43,7 @@ public class Post {
joinColumns = @JoinColumn(name = "post_id"), joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id") inverseJoinColumns = @JoinColumn(name = "tag_id")
) )
private List<Tag> tags = new ArrayList<>(); private Set<Tag> tags = new HashSet<>();
public void addComment(PostComment postComment) { public void addComment(PostComment postComment) {
postComment.setPost(this); postComment.setPost(this);

View File

@ -1,4 +1,4 @@
package dev.struchkov.example.hibernate.nbfe.problem.domain; package dev.struchkov.example.hibernate.nbfe.no_fix_set.domain;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;

View File

@ -1,4 +1,4 @@
package dev.struchkov.example.hibernate.nbfe.problem.domain; package dev.struchkov.example.hibernate.nbfe.no_fix_set.domain;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;

View File

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