|
| 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.propertyref.cachedcollections; |
| 8 | + |
| 9 | +import org.hibernate.Hibernate; |
| 10 | + |
| 11 | +import org.hibernate.testing.TestForIssue; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 15 | +import org.junit.jupiter.api.AfterEach; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +/** |
| 23 | + * Set of tests originally developed to verify and fix HHH-5853 |
| 24 | + * |
| 25 | + * @author Steve Ebersole |
| 26 | + */ |
| 27 | +@TestForIssue(jiraKey = "HHH-5853") |
| 28 | +@DomainModel( |
| 29 | + xmlMappings = "org/hibernate/orm/test/propertyref/cachedcollections/Mappings.hbm.xml" |
| 30 | +) |
| 31 | +@SessionFactory |
| 32 | +public class CachedPropertyRefCollectionTest { |
| 33 | + |
| 34 | + private ManagedObject mo; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setUp(SessionFactoryScope scope) { |
| 38 | + scope.inTransaction( |
| 39 | + session -> { |
| 40 | + mo = new ManagedObject( "test", "test" ); |
| 41 | + mo.getMembers().add( "members" ); |
| 42 | + session.save( mo ); |
| 43 | + } |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterEach |
| 48 | + public void tearDown(SessionFactoryScope scope) { |
| 49 | + scope.inTransaction( |
| 50 | + session -> |
| 51 | + session.delete( mo ) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testRetrievalOfCachedCollectionWithPropertyRefKey(SessionFactoryScope scope) { |
| 57 | + // First attempt to load it via PK lookup |
| 58 | + scope.inTransaction( |
| 59 | + session -> { |
| 60 | + ManagedObject obj = session.get( ManagedObject.class, 1L ); |
| 61 | + assertNotNull( obj ); |
| 62 | + assertTrue( Hibernate.isInitialized( obj ) ); |
| 63 | + obj.getMembers().size(); |
| 64 | + assertTrue( Hibernate.isInitialized( obj.getMembers() ) ); |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + // Now try to access it via natural key |
| 69 | + scope.inTransaction( |
| 70 | + session -> { |
| 71 | + ManagedObject obj = session.bySimpleNaturalId( ManagedObject.class ).load( "test" ); |
| 72 | + assertNotNull( obj ); |
| 73 | + assertTrue( Hibernate.isInitialized( obj ) ); |
| 74 | + obj.getMembers().size(); |
| 75 | + assertTrue( Hibernate.isInitialized( obj.getMembers() ) ); |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
| 80 | + |
0 commit comments