|
| 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.bytecode.enhancement.lazy; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.FetchType; |
| 9 | +import jakarta.persistence.GeneratedValue; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.ManyToOne; |
| 12 | +import jakarta.persistence.OneToMany; |
| 13 | +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.Jira; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +@Jira("https://hibernate.atlassian.net/browse/HHH-13380") |
| 28 | +@DomainModel(annotatedClasses = { |
| 29 | + LazyOneToManySetEqualsTest.ParentEntity.class, |
| 30 | + LazyOneToManySetEqualsTest.ChildEntity.class |
| 31 | +}) |
| 32 | +@SessionFactory |
| 33 | +@BytecodeEnhanced |
| 34 | +public class LazyOneToManySetEqualsTest { |
| 35 | + @Test |
| 36 | + public void testRetrievalOfOneToMany(SessionFactoryScope scope) { |
| 37 | + scope.inTransaction( entityManager -> { |
| 38 | + final ParentEntity parent = entityManager.find( ParentEntity.class, 1L ); |
| 39 | + assertThat( parent.children ).hasSize( 2 ); |
| 40 | + } ); |
| 41 | + } |
| 42 | + |
| 43 | + @BeforeAll |
| 44 | + public void setUp(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( session -> { |
| 46 | + final ParentEntity parent = new ParentEntity(); |
| 47 | + parent.id = 1L; |
| 48 | + session.persist( parent ); |
| 49 | + final ChildEntity child1 = new ChildEntity( "child_1", parent ); |
| 50 | + session.persist( child1 ); |
| 51 | + final ChildEntity child2 = new ChildEntity( "child_2", parent ); |
| 52 | + session.persist( child2 ); |
| 53 | + } ); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterAll |
| 57 | + public void tearDown(SessionFactoryScope scope) { |
| 58 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 59 | + } |
| 60 | + |
| 61 | + @Entity(name = "ParentEntity") |
| 62 | + public static class ParentEntity { |
| 63 | + @Id |
| 64 | + private Long id; |
| 65 | + |
| 66 | + @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) |
| 67 | + private Set<ChildEntity> children = new HashSet<>(); |
| 68 | + } |
| 69 | + |
| 70 | + @Entity(name = "Course") |
| 71 | + public static class ChildEntity { |
| 72 | + @Id |
| 73 | + @GeneratedValue |
| 74 | + private Long id; |
| 75 | + |
| 76 | + private String name; |
| 77 | + |
| 78 | + @ManyToOne(fetch = FetchType.LAZY) |
| 79 | + private ParentEntity parent; |
| 80 | + |
| 81 | + public ChildEntity() { |
| 82 | + } |
| 83 | + |
| 84 | + public ChildEntity(String name, ParentEntity parent) { |
| 85 | + this.name = name; |
| 86 | + this.parent = parent; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean equals(Object o) { |
| 91 | + if ( o == null || getClass() != o.getClass() ) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + ChildEntity that = (ChildEntity) o; |
| 96 | + return name.equals( that.name ) && parent.equals( that.parent ); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int hashCode() { |
| 101 | + int result = name.hashCode(); |
| 102 | + result = 31 * result + parent.hashCode(); |
| 103 | + return result; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments