Affects Hibernate ORM 6.2, probably also 6.1 and 6.0, but definitely not 5.6.
This model:
@Entity(name = "entityA")
public static class EntityA {
@Id
private Integer id;
@OneToOne(mappedBy = "identicallyNamedAssociation", fetch = FetchType.LAZY)
private EntityB b;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public EntityB getB() {
return b;
}
public void setB(EntityB b) {
this.b = b;
}
}
@Entity(name = "entityB")
public static class EntityB {
@Id
private Integer id;
@OneToOne
@JoinColumn(name = "entityA_id")
private EntityA identicallyNamedAssociation;
@ElementCollection
@Embedded
@OrderColumn(name = "idx")
@CollectionTable(name = "elemcollect")
private List<EmbeddableB> elementCollection = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public EntityA getIdenticallyNamedAssociation() {
return identicallyNamedAssociation;
}
public void setIdenticallyNamedAssociation(EntityA a) {
this.identicallyNamedAssociation = a;
}
public List<EmbeddableB> getElementCollection() {
return elementCollection;
}
}
@Embeddable
public static class EmbeddableB {
@OneToOne
@JoinColumn(name = "emb_entityA_id")
private EntityA identicallyNamedAssociation;
public EntityA getIdenticallyNamedAssociation() {
return identicallyNamedAssociation;
}
public void setIdenticallyNamedAssociation(EntityA a) {
this.identicallyNamedAssociation = a;
}
}
Leads to this stack trace:
java.lang.AssertionError
at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.withRegisteredAssociationKeys(ToOneAttributeMapping.java:1492)
at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.generateFetch(ToOneAttributeMapping.java:1232)
at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.generateFetch(ToOneAttributeMapping.java:106)
at org.hibernate.sql.results.graph.FetchParent.generateFetchableFetch(FetchParent.java:105)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.lambda$createFetchableBiConsumer$9(LoaderSelectBuilder.java:842)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.lambda$visitFetches$8(LoaderSelectBuilder.java:681)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.hibernate.metamodel.mapping.internal.EmbeddableMappingTypeImpl.visitAttributeMappings(EmbeddableMappingTypeImpl.java:726)
at org.hibernate.metamodel.mapping.internal.EmbeddableMappingTypeImpl.visitSubParts(EmbeddableMappingTypeImpl.java:736)
at org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart.visitSubParts(EmbeddedCollectionPart.java:286)
at org.hibernate.sql.results.graph.FetchableContainer.visitFetchables(FetchableContainer.java:35)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.visitFetches(LoaderSelectBuilder.java:680)
at org.hibernate.loader.ast.internal.LoaderSqlAstCreationState.visitFetches(LoaderSqlAstCreationState.java:118)
at org.hibernate.sql.results.graph.AbstractFetchParent.afterInitialize(AbstractFetchParent.java:32)
at org.hibernate.sql.results.graph.embeddable.internal.EmbeddableFetchImpl.<init>(EmbeddableFetchImpl.java:75)
at org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart.generateFetch(EmbeddedCollectionPart.java:162)
at org.hibernate.sql.results.graph.FetchParent.generateFetchableFetch(FetchParent.java:105)
at org.hibernate.collection.spi.InitializerProducerBuilder.createListInitializerProducer(InitializerProducerBuilder.java:153)
at org.hibernate.collection.spi.InitializerProducerBuilder.createCollectionTypeWrapperInitializerProducer(InitializerProducerBuilder.java:255)
at org.hibernate.collection.internal.CustomCollectionTypeSemantics.createInitializerProducer(CustomCollectionTypeSemantics.java:79)
at org.hibernate.collection.spi.CollectionSemantics.createInitializerProducer(CollectionSemantics.java:94)
at org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult.<init>(CollectionDomainResult.java:64)
at org.hibernate.metamodel.mapping.internal.PluralAttributeMappingImpl.createDomainResult(PluralAttributeMappingImpl.java:370)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.generateSelect(LoaderSelectBuilder.java:450)
at org.hibernate.loader.ast.internal.LoaderSelectBuilder.createSelect(LoaderSelectBuilder.java:177)
at org.hibernate.loader.ast.internal.CollectionLoaderSingleKey.<init>(CollectionLoaderSingleKey.java:61)
at org.hibernate.persister.collection.AbstractCollectionPersister.createCollectionLoader(AbstractCollectionPersister.java:869)
at org.hibernate.persister.collection.AbstractCollectionPersister.postInstantiate(AbstractCollectionPersister.java:750)
at java.base/java.util.concurrent.ConcurrentHashMap$ValuesView.forEach(ConcurrentHashMap.java:4772)
at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.finishInitialization(MappingMetamodelImpl.java:240)
at org.hibernate.metamodel.internal.RuntimeMetamodelsImpl.finishInitialization(RuntimeMetamodelsImpl.java:60)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:415)
at org.hibernate.testing.orm.junit.SessionFactoryExtension.lambda$createSessionFactoryScope$0(SessionFactoryExtension.java:151)