HHH-18675: Testcase added to Hibernate Project includes a reference testcase without generics.

This commit is contained in:
Leon Schenk 2024-09-30 21:27:56 +02:00 committed by Christian Beikov
parent 6ecc2a6c40
commit fcba4ed25d
2 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,96 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.manytomany.generic;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
@Jpa(
annotatedClasses = {
ManyToManyGenericTest.NodeTree.class,
ManyToManyGenericTest.GenericNode.class
}
)
public class ManyToManyGenericTest {
@Test
void testSelfReferencingGeneric(final EntityManagerFactoryScope scope) {
final UUID treeId = scope.fromTransaction(em -> {
final NodeTree tree = new NodeTree();
final GenericNode<?> root = new GenericNode<>();
root.tree = tree;
final GenericNode<?> branch = new GenericNode<>();
branch.tree = tree;
tree.nodes.add(root);
tree.nodes.add(branch);
root.children.add(branch);
em.persist(tree);
return tree.id;
});
final NodeTree nodeTree = scope.fromEntityManager(em -> em.find(NodeTree.class, treeId));
assertThat(nodeTree, is(notNullValue()));
assertThat(nodeTree.id, is(treeId));
assertThat(nodeTree.nodes, iterableWithSize(2));
assertThat(nodeTree.nodes, containsInAnyOrder(List.of(
hasProperty("children", iterableWithSize(1)),
hasProperty("children", emptyIterable())
)));
}
@Entity(name = "tree")
public static class NodeTree {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
public UUID id;
@OneToMany(mappedBy = "tree", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
public Set<GenericNode<?>> nodes = new HashSet<>();
}
@Entity(name = "node")
public static class GenericNode<T> {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
public UUID id;
@ManyToOne(optional = false)
@JoinColumn(name = "TREE_ID")
public NodeTree tree;
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.DETACH})
@JoinTable(name = "NODE_CHILDREN",
joinColumns = {@JoinColumn(name = "TREE_ID", referencedColumnName = "TREE_ID"), @JoinColumn(name = "NODE_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "CHILD_ID", referencedColumnName = "ID")}
)
private final Set<GenericNode<?>> children = new HashSet<>();
public Set<GenericNode<?>> getChildren() {
return children;
}
}
}

View File

@ -0,0 +1,106 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.manytomany.generic;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.hamcrest.Matchers.notNullValue;
@Jpa(
annotatedClasses = {
ManyToManyNonGenericTest.NodeTree.class,
ManyToManyNonGenericTest.Node.class
}
)
public class ManyToManyNonGenericTest {
@Test
void testSelfReferencingGeneric(final EntityManagerFactoryScope scope) {
final UUID treeId = scope.fromTransaction(em -> {
final NodeTree tree = new NodeTree();
final Node root = new Node();
root.tree = tree;
final Node branch = new Node();
branch.tree = tree;
tree.nodes.add(root);
tree.nodes.add(branch);
root.children.add(branch);
em.persist(tree);
return tree.id;
});
final NodeTree nodeTree = scope.fromEntityManager(em -> em.find(NodeTree.class, treeId));
assertThat(nodeTree, is(notNullValue()));
assertThat(nodeTree.id, is(treeId));
assertThat(nodeTree.nodes, iterableWithSize(2));
assertThat(nodeTree.nodes, containsInAnyOrder(List.of(
hasProperty("children", iterableWithSize(1)),
hasProperty("children", emptyIterable())
)));
}
@Entity(name = "tree")
public static class NodeTree {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
public UUID id;
@OneToMany(mappedBy = "tree", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
public Set<Node> nodes = new HashSet<>();
}
@Entity(name = "node")
public static class Node {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
public UUID id;
@ManyToOne(optional = false)
@JoinColumn(name = "TREE_ID")
public NodeTree tree;
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.DETACH})
@JoinTable(name = "NODE_CHILDREN",
joinColumns = {@JoinColumn(name = "TREE_ID", referencedColumnName = "TREE_ID"), @JoinColumn(name = "NODE_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "CHILD_ID", referencedColumnName = "ID")}
)
private final Set<Node> children = new HashSet<>();
public Set<Node> getChildren() {
return children;
}
@Override
public String toString() {
return "node [%s] parent of %s".formatted(id, children.stream().map(n -> n.id).toList());
}
}
}