|
| 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.any.annotations; |
| 6 | + |
| 7 | +import org.hibernate.LazyInitializationException; |
| 8 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 9 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 17 | + |
| 18 | +@DomainModel( |
| 19 | + annotatedPackageNames = "org.hibernate.orm.test.any.annotations", |
| 20 | + annotatedClasses = { |
| 21 | + StringProperty.class, |
| 22 | + IntegerProperty.class, |
| 23 | + PropertySet.class, |
| 24 | + LazyPropertySet.class, |
| 25 | + } |
| 26 | +) |
| 27 | +@SessionFactory |
| 28 | +@JiraKey( "HHH-13243" ) |
| 29 | +public class AnyFetchEagerTest { |
| 30 | + @AfterEach |
| 31 | + public void dropTestData(SessionFactoryScope scope) { |
| 32 | + scope.inTransaction( |
| 33 | + session -> { |
| 34 | + session.createMutationQuery( "delete StringProperty" ).executeUpdate(); |
| 35 | + session.createMutationQuery( "delete IntegerProperty" ).executeUpdate(); |
| 36 | + session.createMutationQuery( "delete PropertySet" ).executeUpdate(); |
| 37 | + session.createMutationQuery( "delete LazyPropertySet" ).executeUpdate(); |
| 38 | + } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testManyToAnyFetchEager(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( s -> { |
| 46 | + PropertySet set = new PropertySet( "string" ); |
| 47 | + Property property = new StringProperty( "name", "Alex" ); |
| 48 | + set.addGeneralProperty( property ); |
| 49 | + s.persist( set ); |
| 50 | + } ); |
| 51 | + |
| 52 | + PropertySet result = scope.fromTransaction( |
| 53 | + s -> s.createQuery( "select s from PropertySet s where name = :name", PropertySet.class ) |
| 54 | + .setParameter( "name", "string" ) |
| 55 | + .getSingleResult() ); |
| 56 | + |
| 57 | + assertThat( result ).isNotNull(); |
| 58 | + assertThat( result.getGeneralProperties() ).isNotNull(); |
| 59 | + assertThat( result.getGeneralProperties().size() ).isEqualTo( 1 ); |
| 60 | + assertThat( result.getGeneralProperties().get(0).asString() ).isEqualTo( "Alex" ); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testManyToAnyFetchLazy(SessionFactoryScope scope) { |
| 65 | + scope.inTransaction( s -> { |
| 66 | + LazyPropertySet set = new LazyPropertySet( "string" ); |
| 67 | + Property property = new StringProperty( "name", "Alex" ); |
| 68 | + set.addGeneralProperty( property ); |
| 69 | + s.persist( set ); |
| 70 | + } ); |
| 71 | + |
| 72 | + LazyPropertySet result = scope.fromTransaction( |
| 73 | + s -> s.createQuery( "select s from LazyPropertySet s where name = :name", LazyPropertySet.class ) |
| 74 | + .setParameter( "name", "string" ) |
| 75 | + .getSingleResult() ); |
| 76 | + |
| 77 | + assertThat( result ).isNotNull(); |
| 78 | + assertThat( result.getGeneralProperties() ).isNotNull(); |
| 79 | + |
| 80 | + try { |
| 81 | + result.getGeneralProperties().get(0); |
| 82 | + Assertions.fail( "should not get the property string after session closed." ); |
| 83 | + } |
| 84 | + catch (LazyInitializationException e) { |
| 85 | + // expected |
| 86 | + } |
| 87 | + catch (Exception e) { |
| 88 | + Assertions.fail( "should not throw exception other than LazyInitializationException." ); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments