From 57b0983003677d9c7245938e83b466e9292c342e Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sat, 2 Jul 2022 18:00:56 +0300 Subject: [PATCH] Problem Variant --- .../hibernate/nbfe/problem/ProblemMain.java | 31 ++++++++++ .../hibernate/nbfe/problem/domain/Post.java | 57 +++++++++++++++++++ .../nbfe/problem/domain/PostComment.java | 24 ++++++++ .../hibernate/nbfe/problem/domain/Tag.java | 20 +++++++ 4 files changed, 132 insertions(+) create mode 100644 src/main/java/dev/struchkov/example/hibernate/nbfe/problem/ProblemMain.java create mode 100644 src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Post.java create mode 100644 src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/PostComment.java create mode 100644 src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Tag.java diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/ProblemMain.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/ProblemMain.java new file mode 100644 index 0000000..5964c05 --- /dev/null +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/ProblemMain.java @@ -0,0 +1,31 @@ +package dev.struchkov.example.hibernate.nbfe.problem; + +import dev.struchkov.example.hibernate.nbfe.problem.domain.Post; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; + +import java.util.List; + +public class ProblemMain { + + public static void main(String[] args) { + final EntityManagerFactory emf = Persistence.createEntityManagerFactory("Blog"); + final EntityManager entityManager = emf.createEntityManager(); + + 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(); + } + + +} diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Post.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Post.java new file mode 100644 index 0000000..761ba30 --- /dev/null +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Post.java @@ -0,0 +1,57 @@ +package dev.struchkov.example.hibernate.nbfe.problem.domain; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.ManyToMany; +import jakarta.persistence.OneToMany; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Entity +@Getter +@Setter +public class Post { + + @Id + @GeneratedValue + private Long id; + + private String title; + + @OneToMany( + mappedBy = "post", + cascade = CascadeType.ALL, + orphanRemoval = true + ) + private List comments = new ArrayList<>(); + + @ManyToMany( + cascade = { + CascadeType.PERSIST, + CascadeType.MERGE + } + ) + @JoinTable( + name = "post_tag", + joinColumns = @JoinColumn(name = "post_id"), + inverseJoinColumns = @JoinColumn(name = "tag_id") + ) + private List tags = new ArrayList<>(); + + public void addComment(PostComment postComment) { + postComment.setPost(this); + comments.add(postComment); + } + + public void addTag(Tag tag) { + tags.add(tag); + } + +} diff --git a/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/PostComment.java b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/PostComment.java new file mode 100644 index 0000000..5e67aca --- /dev/null +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/PostComment.java @@ -0,0 +1,24 @@ +package dev.struchkov.example.hibernate.nbfe.problem.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import lombok.Getter; +import lombok.Setter; + +@Entity +@Getter +@Setter +public class PostComment { + + @Id + @GeneratedValue + private Long id; + + @ManyToOne + private Post post; + + private String review; + +} 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 new file mode 100644 index 0000000..b321125 --- /dev/null +++ b/src/main/java/dev/struchkov/example/hibernate/nbfe/problem/domain/Tag.java @@ -0,0 +1,20 @@ +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; + +@Entity +@Getter +@Setter +public class Tag { + + @Id + @GeneratedValue + private Long id; + + private String name; + +}