|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.bootstrap.binding.hbm.cid.nonaggregated.dynamic; |
| 6 | + |
| 7 | +import org.hibernate.cfg.AvailableSettings; |
| 8 | +import org.hibernate.metamodel.mapping.AttributeMapping; |
| 9 | +import org.hibernate.metamodel.mapping.EntityIdentifierMapping; |
| 10 | +import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping; |
| 11 | +import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping; |
| 12 | +import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping; |
| 13 | +import org.hibernate.persister.entity.EntityPersister; |
| 14 | + |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 16 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.hibernate.testing.orm.junit.Setting; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 23 | +import static org.hamcrest.CoreMatchers.is; |
| 24 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | + |
| 27 | +/** |
| 28 | + * Note that this test uses a composite-id with key-many-to-one as part of a |
| 29 | + * dynamic model, which is the main construct needed by hibernate-envers |
| 30 | + * |
| 31 | + * @author Steve Ebersole |
| 32 | + */ |
| 33 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 34 | +@ServiceRegistry( |
| 35 | + settings = @Setting( name = AvailableSettings.HBM2DDL_AUTO, value = "create-drop" ) |
| 36 | +) |
| 37 | +@DomainModel( xmlMappings = "org/hibernate/orm/test/bootstrap/binding/hbm/cid/nonaggregated/dynamic/DynamicCompositeIdManyToOne.hbm.xml" ) |
| 38 | +@SessionFactory |
| 39 | +public class DynamicCompositeIdManyToOneTests { |
| 40 | + @Test |
| 41 | + void testBinding(SessionFactoryScope factoryScope) { |
| 42 | + final EntityPersister entityDescriptor = factoryScope.getSessionFactory() |
| 43 | + .getRuntimeMetamodels() |
| 44 | + .getMappingMetamodel() |
| 45 | + .findEntityDescriptor( "DynamicCompositeIdManyToOne" ); |
| 46 | + |
| 47 | + assertThat( entityDescriptor.getNumberOfAttributeMappings(), is( 1 ) ); |
| 48 | + |
| 49 | + final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping(); |
| 50 | + assertThat( identifierMapping, instanceOf( NonAggregatedIdentifierMapping.class ) ); |
| 51 | + final NonAggregatedIdentifierMapping cid = (NonAggregatedIdentifierMapping) identifierMapping; |
| 52 | + assertThat( cid.getEmbeddableTypeDescriptor().getNumberOfAttributeMappings(), is( 2 ) ); |
| 53 | + |
| 54 | + final AttributeMapping key1 = cid.getEmbeddableTypeDescriptor().findAttributeMapping( "key1" ); |
| 55 | + assertThat( key1, notNullValue() ); |
| 56 | + assertThat( key1, instanceOf( BasicAttributeMapping.class ) ); |
| 57 | + |
| 58 | + final AttributeMapping key2 = cid.getEmbeddableTypeDescriptor().findAttributeMapping( "key2" ); |
| 59 | + assertThat( key2, notNullValue() ); |
| 60 | + assertThat( key2, instanceOf( ToOneAttributeMapping.class ) ); |
| 61 | + |
| 62 | + final AttributeMapping attr1 = entityDescriptor.findAttributeMapping( "attr1" ); |
| 63 | + assertThat( attr1, notNullValue() ); |
| 64 | + assertThat( attr1, instanceOf( BasicAttributeMapping.class ) ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testFullQueryReference(SessionFactoryScope scope) { |
| 69 | + scope.inTransaction( |
| 70 | + (session) -> { |
| 71 | + session.createQuery( "select e from DynamicCompositeIdManyToOne e" ).list(); |
| 72 | + session.createQuery( "select e from DynamicCompositeIdManyToOne e where e.id.key1 = 1" ).list(); |
| 73 | + session.createQuery( "select e from DynamicCompositeIdManyToOne e where e.id.key2.name = 'abc'" ).list(); |
| 74 | + } |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testEmbeddedQueryReference(SessionFactoryScope scope) { |
| 80 | + scope.inTransaction( |
| 81 | + (session) -> { |
| 82 | + session.createQuery( "select e from DynamicCompositeIdManyToOne e" ).list(); |
| 83 | + session.createQuery( "select e from DynamicCompositeIdManyToOne e where e.key1 = 1" ).list(); |
| 84 | + session.createQuery( "select e from DynamicCompositeIdManyToOne e where e.key2.name = 'abc'" ).list(); |
| 85 | + } |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
0 commit comments