|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.inheritance; |
| 8 | + |
| 9 | +import java.util.LinkedList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.hibernate.Hibernate; |
| 13 | + |
| 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 jakarta.persistence.CascadeType; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.FetchType; |
| 25 | +import jakarta.persistence.GeneratedValue; |
| 26 | +import jakarta.persistence.Id; |
| 27 | +import jakarta.persistence.Inheritance; |
| 28 | +import jakarta.persistence.InheritanceType; |
| 29 | +import jakarta.persistence.ManyToOne; |
| 30 | +import jakarta.persistence.OneToMany; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Marco Belladelli |
| 36 | + */ |
| 37 | +@DomainModel( annotatedClasses = { |
| 38 | + JoinedInheritanceCircularBiDirectionalFetchTest.Animal.class, |
| 39 | + JoinedInheritanceCircularBiDirectionalFetchTest.Cat.class, |
| 40 | + JoinedInheritanceCircularBiDirectionalFetchTest.Leg.class, |
| 41 | +} ) |
| 42 | +@SessionFactory |
| 43 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-17832" ) |
| 44 | +public class JoinedInheritanceCircularBiDirectionalFetchTest { |
| 45 | + @Test |
| 46 | + public void testJoinSelectCat(SessionFactoryScope scope) { |
| 47 | + scope.inTransaction( session -> { |
| 48 | + final Cat result = session.createQuery( |
| 49 | + "select cat from Cat cat join cat.legs leg", |
| 50 | + Cat.class |
| 51 | + ).getSingleResult(); |
| 52 | + assertThat( result.getName() ).isEqualTo( "gatta" ); |
| 53 | + assertThat( result.getLegs() ).hasSize( 2 ).extracting( Leg::getCat ).containsOnly( result ); |
| 54 | + } ); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testJoinSelectLeg(SessionFactoryScope scope) { |
| 59 | + scope.inTransaction( session -> { |
| 60 | + final List<Leg> resultList = session.createQuery( |
| 61 | + "select leg from Cat cat join cat.legs leg", |
| 62 | + Leg.class |
| 63 | + ).getResultList(); |
| 64 | + assertThat( resultList ) |
| 65 | + .hasSize( 2 ).allMatch( l -> Hibernate.isInitialized( l.getCat() ) ) |
| 66 | + .extracting( Leg::getCat ).containsOnly( resultList.get( 0 ).getCat() ) |
| 67 | + .extracting( Cat::getName ).containsOnly( "gatta" ); |
| 68 | + } ); |
| 69 | + } |
| 70 | + |
| 71 | + @BeforeAll |
| 72 | + public void setUp(SessionFactoryScope scope) { |
| 73 | + scope.inTransaction( session -> { |
| 74 | + final Leg leg1 = new Leg( "leg1" ); |
| 75 | + final Leg leg2 = new Leg( "leg2" ); |
| 76 | + final Cat cat = new Cat(); |
| 77 | + cat.setName( "gatta" ); |
| 78 | + cat.addToLegs( leg1 ); |
| 79 | + cat.addToLegs( leg2 ); |
| 80 | + session.persist( cat ); |
| 81 | + } ); |
| 82 | + } |
| 83 | + |
| 84 | + @AfterAll |
| 85 | + public void tearDown(SessionFactoryScope scope) { |
| 86 | + scope.inTransaction( session -> { |
| 87 | + session.createMutationQuery( "delete from Leg" ).executeUpdate(); |
| 88 | + session.createMutationQuery( "delete from Animal" ).executeUpdate(); |
| 89 | + } ); |
| 90 | + } |
| 91 | + |
| 92 | + @Entity( name = "Animal" ) |
| 93 | + @Inheritance( strategy = InheritanceType.JOINED ) |
| 94 | + public static class Animal { |
| 95 | + @Id |
| 96 | + @GeneratedValue |
| 97 | + protected Long id; |
| 98 | + |
| 99 | + protected String name; |
| 100 | + |
| 101 | + public long getId() { |
| 102 | + return id; |
| 103 | + } |
| 104 | + |
| 105 | + public String getName() { |
| 106 | + return name; |
| 107 | + } |
| 108 | + |
| 109 | + public void setName(String name) { |
| 110 | + this.name = name; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Entity( name = "Cat" ) |
| 115 | + public static class Cat extends Animal { |
| 116 | + @OneToMany( mappedBy = "cat", cascade = CascadeType.PERSIST ) |
| 117 | + private List<Leg> legs = new LinkedList<>(); |
| 118 | + |
| 119 | + public List<Leg> getLegs() { |
| 120 | + return this.legs; |
| 121 | + } |
| 122 | + |
| 123 | + public void addToLegs(Leg person) { |
| 124 | + legs.add( person ); |
| 125 | + person.cat = this; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Entity( name = "Leg" ) |
| 130 | + public static class Leg { |
| 131 | + @Id |
| 132 | + @GeneratedValue |
| 133 | + private long id; |
| 134 | + |
| 135 | + @ManyToOne( fetch = FetchType.LAZY ) |
| 136 | + private Cat cat; |
| 137 | + |
| 138 | + private String name; |
| 139 | + |
| 140 | + public Leg() { |
| 141 | + } |
| 142 | + |
| 143 | + public Leg(String name) { |
| 144 | + this.name = name; |
| 145 | + } |
| 146 | + |
| 147 | + public Cat getCat() { |
| 148 | + return cat; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments