This commit is contained in:
Struchkov Mark 2022-07-02 19:08:15 +03:00
parent 75b07336e1
commit 14ba09b89f
2 changed files with 58 additions and 2 deletions

View File

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

View File

@ -1,7 +1,6 @@
package dev.struchkov.example.hibernate.nbfe.problem.domain; package dev.struchkov.example.hibernate.nbfe.problem.domain;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -12,7 +11,6 @@ import lombok.Setter;
public class Tag { public class Tag {
@Id @Id
@GeneratedValue
private Long id; private Long id;
private String name; private String name;