Skip to content

Commit 0926c86

Browse files
committed
HHH-19324 - Switch tests using hbm.xml to use mapping.xml
1 parent 5a69197 commit 0926c86

File tree

9 files changed

+213
-252
lines changed

9 files changed

+213
-252
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataContext.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,13 @@ private <T> void applyIdMetadata(PersistentClass persistentClass, IdentifiableDo
525525
if ( identifierMapper != null ) {
526526
cidProperties = identifierMapper.getProperties();
527527
propertySpan = identifierMapper.getPropertySpan();
528-
idClassType = applyIdClassMetadata( (Component) persistentClass.getIdentifier() );
528+
if ( identifierMapper.getComponentClassName() == null ) {
529+
// support for no id-class, especially for dynamic models
530+
idClassType = null;
531+
}
532+
else {
533+
idClassType = applyIdClassMetadata( (Component) persistentClass.getIdentifier() );
534+
}
529535
}
530536
else {
531537
cidProperties = compositeId.getProperties();

hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/hbm/cid/nonaggregated/dynamic/DynamicCompositeIdBasicBindingTests.java

Lines changed: 0 additions & 64 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.persister.entity.EntityPersister;
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.ServiceRegistry;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.hibernate.testing.orm.junit.Setting;
17+
import org.junit.jupiter.api.Test;
18+
19+
import static org.hamcrest.CoreMatchers.instanceOf;
20+
import static org.hamcrest.CoreMatchers.is;
21+
import static org.hamcrest.CoreMatchers.notNullValue;
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
24+
/**
25+
* @author Steve Ebersole
26+
*/
27+
@SuppressWarnings("JUnitMalformedDeclaration")
28+
@ServiceRegistry(
29+
settings = @Setting( name = AvailableSettings.HBM2DDL_AUTO, value = "create-drop" )
30+
)
31+
@DomainModel( xmlMappings = "org/hibernate/orm/test/bootstrap/binding/hbm/cid/nonaggregated/dynamic/DynamicCompositeIdBasic.hbm.xml" )
32+
@SessionFactory
33+
public class DynamicCompositeIdBasicTests {
34+
@Test
35+
void testBinding(SessionFactoryScope factoryScope) {
36+
final EntityPersister entityDescriptor = factoryScope.getSessionFactory()
37+
.getRuntimeMetamodels()
38+
.getMappingMetamodel()
39+
.findEntityDescriptor( "DynamicCompositeIdBasic" );
40+
41+
assertThat( entityDescriptor.getNumberOfAttributeMappings(), is( 1 ) );
42+
43+
final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
44+
assertThat( identifierMapping, instanceOf( NonAggregatedIdentifierMapping.class ) );
45+
final NonAggregatedIdentifierMapping cid = (NonAggregatedIdentifierMapping) identifierMapping;
46+
assertThat( cid.getEmbeddableTypeDescriptor().getNumberOfAttributeMappings(), is( 2 ) );
47+
48+
final AttributeMapping key1 = cid.getEmbeddableTypeDescriptor().findAttributeMapping( "key1" );
49+
assertThat( key1, notNullValue() );
50+
51+
final AttributeMapping key2 = cid.getEmbeddableTypeDescriptor().findAttributeMapping( "key2" );
52+
assertThat( key2, notNullValue() );
53+
54+
final AttributeMapping attr1 = entityDescriptor.findAttributeMapping( "attr1" );
55+
assertThat( attr1, notNullValue() ); }
56+
57+
@Test
58+
public void testFullQueryReference(SessionFactoryScope scope) {
59+
scope.inTransaction(
60+
session -> session.createQuery( "from DynamicCompositeIdBasic e where e.id.key1 = 1" ).list()
61+
);
62+
}
63+
64+
@Test
65+
public void testEmbeddedQueryReference(SessionFactoryScope scope) {
66+
scope.inTransaction(
67+
session -> session.createQuery( "from DynamicCompositeIdBasic e where e.key1 = 1" ).list()
68+
);
69+
}
70+
}

hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/hbm/cid/nonaggregated/dynamic/DynamicCompositeIdBasicUsageTests.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/binding/hbm/cid/nonaggregated/dynamic/DynamicCompositeIdManyToOneBindingTests.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)