mirror of
https://github.com/Example-uPagge/hibernate-multiple-bag-fetch-exception.git
synced 2024-06-15 10:55:24 +03:00
Problem Variant
This commit is contained in:
parent
0077065d44
commit
57b0983003
@ -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<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();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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<PostComment> 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<Tag> tags = new ArrayList<>();
|
||||
|
||||
public void addComment(PostComment postComment) {
|
||||
postComment.setPost(this);
|
||||
comments.add(postComment);
|
||||
}
|
||||
|
||||
public void addTag(Tag tag) {
|
||||
tags.add(tag);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user