From 14ba09b89fa0ce79deb3985bea23c10cb5979c41 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sat, 2 Jul 2022 19:08:15 +0300 Subject: [PATCH] update --- .../hibernate/nbfe/problem/GeneratePost.java | 58 +++++++++++++++++++ .../hibernate/nbfe/problem/domain/Tag.java | 2 - 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/main/java/dev/struchkov/example/hibernate/nbfe/problem/GeneratePost.java diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/GeneratePost.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/GeneratePost.java new file mode 100644 index 0000000..992aba4 --- /dev/null +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/GeneratePost.java @@ -0,0 +1,58 @@ +package dev.struchkov.example.hibernate.nbfe.problem; + +import dev.struchkov.example.hibernate.nbfe.problem.domain.Post; +import dev.struchkov.example.hibernate.nbfe.problem.domain.PostComment; +import dev.struchkov.example.hibernate.nbfe.problem.domain.Tag; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.LongStream; + +public class GeneratePost { + + public static void main(String[] args) { + + final EntityManagerFactory emf = Persistence.createEntityManagerFactory("Blog"); + final EntityManager entityManager = emf.createEntityManager(); + + entityManager.getTransaction().begin(); + + for (long j = 0; j < 20; j++) { + final Tag tag = new Tag(); + tag.setId(j); + tag.setName("Tag " + j); + entityManager.merge(tag); + } + + entityManager.getTransaction().commit(); + + entityManager.getTransaction().begin(); + + final List tags = entityManager.createQuery("SELECT t FROM Tag t where t.id IN :id", Tag.class) + .setParameter("id", LongStream.range(0, 19).boxed().collect(Collectors.toSet())) + .getResultList(); + + for (int i = 0; i < 50; i++) { + final Post post = new Post(); + post.setTitle("Пост " + i); + + for (int j = 0; j < 1000; j++) { + final PostComment postComment = new PostComment(); + postComment.setReview("Комментарий " + j); + post.addComment(postComment); + } + + tags.forEach(post::addTag); + + entityManager.merge(post); + } + + entityManager.getTransaction().commit(); + + } + +} diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Tag.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Tag.java index b321125..5d4cfbc 100644 --- a/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Tag.java +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Tag.java @@ -1,7 +1,6 @@ package dev.struchkov.example.hibernate.nbfe.problem.domain; import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import lombok.Getter; import lombok.Setter; @@ -12,7 +11,6 @@ import lombok.Setter; public class Tag { @Id - @GeneratedValue private Long id; private String name;