Hibernate 6 create EMF exception

Hi. I’m new to this forum, so I hope I framed my issue properly.

I get an UnknownTableReferenceException when creating an EMF, but only when using compile-time enhancement.

Unable to determine TableReference (customer) for treat(DistributorCompany.scheduleTargets.{element}.owner as CustomerCompany).allowWebImpersonation

The interesting thing is that when I omit the compile-time enhancement, the problem goes away. But when I compare the enhanced classes between compile-time and run-time enhancement, they look identical. I’m using Hibernate 6.1.6. The problem goes away in Hibernate 5.6.14

I reproduced the problem in a small project - GitHub - robscala/UnknownTableReference: Reproduces an exception when creating an EMF

Here’s a brief version of my code. There is a base class and two inherited classes using the JOIN strategy. The exception indicates a problem with one of the subclass fields.

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="CompanyType", discriminatorType=DiscriminatorType.INTEGER)
public abstract class Company {
    @Id @GeneratedValue(strategy= GenerationType.IDENTITY)
    protected long id;

    protected String name;
}

@Entity
@DiscriminatorValue("9")
@Table(name = "customer")
public class CustomerCompany extends Company {
    private Boolean allowWebImpersonation;
}

@Entity
@DiscriminatorValue("2")
@Table(name="distributor_company")
public class DistributorCompany extends Company {
    @OneToMany(mappedBy="owner", orphanRemoval=true, cascade= CascadeType.ALL)
    private List<ScheduleTarget> scheduleTargets = new ArrayList<>();

    public List<ScheduleTarget> getScheduleTargets() {
        return scheduleTargets;
    }
}

@Entity
@Table(name="schedule_target")
public class ScheduleTarget {
    @Id @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id;

    @ManyToOne(fetch= FetchType.LAZY)
    @JoinColumn(name = "OWNER_ID", foreignKey = @ForeignKey())
    private Company owner;
}

The full exception:

Exception in thread "main" jakarta.persistence.PersistenceException: [PersistenceUnit: InsideTrack] Unable to build Hibernate SessionFactory
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1509)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1430)
	at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:55)
	at jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:80)
	at Report.run(Report.java:15)
	at Report.main(Report.java:26)
Caused by: org.hibernate.sql.ast.tree.from.UnknownTableReferenceException: Unable to determine TableReference (`customer`) for `treat(DistributorCompany.scheduleTargets.{element}.owner as CustomerCompany).allowWebImpersonation`
	at org.hibernate.sql.ast.tree.from.AbstractColumnReferenceQualifier.resolveTableReference(AbstractColumnReferenceQualifier.java:45)
	at org.hibernate.metamodel.mapping.internal.BasicAttributeMapping.resolveSqlSelection(BasicAttributeMapping.java:256)
	at org.hibernate.metamodel.mapping.internal.BasicAttributeMapping.generateFetch(BasicAttributeMapping.java:321)
	at org.hibernate.sql.results.graph.FetchParent.generateFetchableFetch(FetchParent.java:105)
	at org.hibernate.loader.ast.internal.LoaderSelectBuilder.lambda$createFetchableBiConsumer$7(LoaderSelectBuilder.java:846)
	at org.hibernate.loader.ast.internal.LoaderSelectBuilder.visitFetches(LoaderSelectBuilder.java:684)
	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.entity.AbstractEntityResultGraphNode.afterInitialize(AbstractEntityResultGraphNode.java:100)
	at org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl.<init>(EntityFetchJoinedImpl.java:54)
	at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.createCircularBiDirectionalFetch(ToOneAttributeMapping.java:1058)
	at org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping.resolveCircularFetch(ToOneAttributeMapping.java:830)
	at org.hibernate.loader.ast.internal.LoaderSelectBuilder.lambda$createFetchableBiConsumer$7(LoaderSelectBuilder.java:813)
	at org.hibernate.loader.ast.internal.LoaderSelectBuilder.visitFetches(LoaderSelectBuilder.java:684)
	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.entity.AbstractEntityResultGraphNode.afterInitialize(AbstractEntityResultGraphNode.java:100)
	at org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl.<init>(EntityFetchJoinedImpl.java:76)
	at org.hibernate.metamodel.mapping.internal.EntityCollectionPart.generateFetch(EntityCollectionPart.java:513)
	at org.hibernate.metamodel.mapping.internal.EntityCollectionPart.generateFetch(EntityCollectionPart.java:82)
	at org.hibernate.sql.results.graph.FetchParent.generateFetchableFetch(FetchParent.java:105)
	at org.hibernate.collection.spi.InitializerProducerBuilder.createBagInitializerProducer(InitializerProducerBuilder.java:121)
	at org.hibernate.collection.spi.InitializerProducerBuilder.createCollectionTypeWrapperInitializerProducer(InitializerProducerBuilder.java:245)
	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:427)
	at org.hibernate.loader.ast.internal.LoaderSelectBuilder.createSelect(LoaderSelectBuilder.java:177)
	at org.hibernate.persister.entity.AbstractEntityPersister.generateLazySelectStringsByFetchGroup(AbstractEntityPersister.java:1235)
	at org.hibernate.persister.entity.AbstractEntityPersister.doLateInit(AbstractEntityPersister.java:4326)
	at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:4352)
	at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.finishInitialization(MappingMetamodelImpl.java:236)
	at org.hibernate.metamodel.internal.RuntimeMetamodelsImpl.finishInitialization(RuntimeMetamodelsImpl.java:60)
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:311)
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:415)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1425)
	... 4 more

I prefer not to use runtime enhancement.

Hi there. This is a bug. Please create a JIRA issue for this.

Thanks. Here’s the ticket: [HHH-15906] - Hibernate JIRA

1 Like