|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.manytomany.generic; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.FetchType; |
| 10 | +import jakarta.persistence.GeneratedValue; |
| 11 | +import jakarta.persistence.GenerationType; |
| 12 | +import jakarta.persistence.Id; |
| 13 | +import jakarta.persistence.JoinColumn; |
| 14 | +import jakarta.persistence.JoinTable; |
| 15 | +import jakarta.persistence.ManyToMany; |
| 16 | +import jakarta.persistence.ManyToOne; |
| 17 | +import jakarta.persistence.OneToMany; |
| 18 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 19 | +import org.hibernate.testing.orm.junit.Jpa; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.UUID; |
| 26 | + |
| 27 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 29 | +import static org.hamcrest.Matchers.emptyIterable; |
| 30 | +import static org.hamcrest.Matchers.hasProperty; |
| 31 | +import static org.hamcrest.Matchers.is; |
| 32 | +import static org.hamcrest.Matchers.iterableWithSize; |
| 33 | +import static org.hamcrest.Matchers.notNullValue; |
| 34 | + |
| 35 | +@Jpa( |
| 36 | + annotatedClasses = { |
| 37 | + ManyToManyNonGenericTest.NodeTree.class, |
| 38 | + ManyToManyNonGenericTest.Node.class |
| 39 | + } |
| 40 | +) |
| 41 | +public class ManyToManyNonGenericTest { |
| 42 | + |
| 43 | + @Test |
| 44 | + void testSelfReferencingGeneric(final EntityManagerFactoryScope scope) { |
| 45 | + final UUID treeId = scope.fromTransaction(em -> { |
| 46 | + final NodeTree tree = new NodeTree(); |
| 47 | + final Node root = new Node(); |
| 48 | + root.tree = tree; |
| 49 | + final Node branch = new Node(); |
| 50 | + branch.tree = tree; |
| 51 | + tree.nodes.add(root); |
| 52 | + tree.nodes.add(branch); |
| 53 | + root.children.add(branch); |
| 54 | + em.persist(tree); |
| 55 | + return tree.id; |
| 56 | + }); |
| 57 | + |
| 58 | + final NodeTree nodeTree = scope.fromEntityManager(em -> em.find(NodeTree.class, treeId)); |
| 59 | + |
| 60 | + assertThat(nodeTree, is(notNullValue())); |
| 61 | + assertThat(nodeTree.id, is(treeId)); |
| 62 | + assertThat(nodeTree.nodes, iterableWithSize(2)); |
| 63 | + assertThat(nodeTree.nodes, containsInAnyOrder(List.of( |
| 64 | + hasProperty("children", iterableWithSize(1)), |
| 65 | + hasProperty("children", emptyIterable()) |
| 66 | + ))); |
| 67 | + } |
| 68 | + |
| 69 | + @Entity(name = "tree") |
| 70 | + public static class NodeTree { |
| 71 | + @Id |
| 72 | + @GeneratedValue(strategy = GenerationType.UUID) |
| 73 | + public UUID id; |
| 74 | + |
| 75 | + @OneToMany(mappedBy = "tree", fetch = FetchType.EAGER, cascade = CascadeType.ALL) |
| 76 | + public Set<Node> nodes = new HashSet<>(); |
| 77 | + } |
| 78 | + |
| 79 | + @Entity(name = "node") |
| 80 | + public static class Node { |
| 81 | + |
| 82 | + @Id |
| 83 | + @GeneratedValue(strategy = GenerationType.UUID) |
| 84 | + public UUID id; |
| 85 | + |
| 86 | + @ManyToOne(optional = false) |
| 87 | + @JoinColumn(name = "TREE_ID") |
| 88 | + public NodeTree tree; |
| 89 | + |
| 90 | + @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.DETACH}) |
| 91 | + @JoinTable(name = "NODE_CHILDREN", |
| 92 | + joinColumns = {@JoinColumn(name = "TREE_ID", referencedColumnName = "TREE_ID"), @JoinColumn(name = "NODE_ID", referencedColumnName = "ID")}, |
| 93 | + inverseJoinColumns = {@JoinColumn(name = "CHILD_ID", referencedColumnName = "ID")} |
| 94 | + ) |
| 95 | + private final Set<Node> children = new HashSet<>(); |
| 96 | + |
| 97 | + public Set<Node> getChildren() { |
| 98 | + return children; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String toString() { |
| 103 | + return "node [%s] parent of %s".formatted(id, children.stream().map(n -> n.id).toList()); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments