Cannot invoke "org.hibernate.sql.ast.tree.from.TableReference.getIdentificationVariable()" because "tableReference" is null

Hello,
I am currently upgrading to Hibernate 6.1.7.Final from 5.6.15.Final. I am having the following error:

Cannot invoke "org.hibernate.sql.ast.tree.from.TableReference.getIdentificationVariable()" because "tableReference" is null
	at org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey(SqlExpressionResolver.java:56) ~[?:?]
	at org.hibernate.metamodel.mapping.ordering.ast.ColumnReference.resolve(ColumnReference.java:62) ~[?:?]
	at org.hibernate.metamodel.mapping.ordering.ast.ColumnReference.apply(ColumnReference.java:95) ~[?:?]
	at org.hibernate.metamodel.mapping.ordering.OrderByFragmentImpl.apply(OrderByFragmentImpl.java:35) ~[?:?]
	at org.hibernate.loader.ast.internal.LoaderSelectBuilder.lambda$generateSelect$1(LoaderSelectBuilder.java:487) ~[?:?]
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[?:?]
	at org.hibernate.loader.ast.internal.LoaderSelectBuilder.generateSelect(LoaderSelectBuilder.java:486) ~[?:?]
	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:4780) ~[?:?]
	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:311) ~[?:?]
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:415) ~[?:?]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1423) ~[?:?]
	at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:142) ~[?:?]
	at org.apache.openejb.assembler.classic.EntityManagerFactoryCallable.call(EntityManagerFactoryCallable.java:112) ~[openejb-core-9.1.1.jar:9.1.1]
	at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.createDelegate(ReloadableEntityManagerFactory.java:134) ~[openejb-core-9.1.1.jar:9.1.1]
	at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.<init>(ReloadableEntityManagerFactory.java:105) ~[openejb-core-9.1.1.jar:9.1.1]
	at org.apache.openejb.assembler.classic.PersistenceBuilder.createEntityManagerFactory(PersistenceBuilder.java:157) ~[openejb-core-9.1.1.jar:9.1.1]
	at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:927) ~[openejb-core-9.1.1.jar:9.1.1]

I have identified that the error is caused by using @Inheritance(strategy = InheritanceType.JOINED) and @ManyToMany. Followings are the classes which demonstrate the relationships:

@MappedSuperclass
@SuppressWarnings("unused")
public abstract class AbstractEntity implements Serializable {
    @Serial
    private static final long serialVersionUID = -4074532875226460060L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "OID", nullable = false, updatable = false)
    protected Long oid;

    @Version
    @Column(name = "VERSION", nullable = false)
    protected Long version;

    @Column(name = "LAST_MOD_TIME", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    protected Date lastModTime;

    @Column(name = "CREATION_TIME", nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    protected Date creationTime;

    public AbstractEntity() {

    }

    public Long getOid() {
        return this.oid;
    }

    public void setOid(Long oid) {
        this.oid = oid;
    }

    public Date getLastModTime() {
        return this.lastModTime;
    }

    public void setLastModTime(Date lastModTime) {
        this.lastModTime = lastModTime;
    }

    public Date getCreationTime() {
        return this.creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

    @PrePersist
    void onPrePersist() {
        if (this.creationTime == null) {
            this.creationTime = new Date();
            this.lastModTime = this.creationTime;
        }
    }

    @PreUpdate
    void onPreUpdate() {
        this.lastModTime = new Date();
    }

    public Long getVersion() {
        return this.Version;
    }

    public void setVersion(Long version) {
        this.Version = Version;
    }
}
@Entity
@Table(name = "HOUSE")
@Inheritance(strategy = InheritanceType.JOINED)
public class House extends AbstractEntity {

    @Serial
    private static final long serialVersionUID = 1176085504819465437L;
}
@Entity
@Table(name = "APARTMENT")
public class Apartment extends House {

    @Serial
    private static final long serialVersionUID = -3952169407737015438L;

    @ManyToMany(mappedBy = "apartments", fetch = FetchType.LAZY)
    private Set<Person> persons;

    public Set<Person> getPersons() {
        return persons;
    }

    public void setPersons(Set<Person> persons) {
        this.persons = persons;
    }
}
@Entity
@Table(name = "PERSON")
public class Person extends AbstractEntity {

    @Serial
    private static final long serialVersionUID = 1457647109183071600L;

    // @formatter:off
    @OrderBy
    @JoinTable(
        name = "PERSON_APARTMENT",
        joinColumns = {
            @JoinColumn(
                name = "PERSON_OID",
                referencedColumnName = "OID",
                nullable = false
            )
        },
        inverseJoinColumns = {
            @JoinColumn(
                name = "APARTMENT_OID",
                referencedColumnName = "OID",
                nullable = false
            )
        }
    )
    @ManyToMany(fetch = FetchType.LAZY)
    // @formatter:on
    private Set<Apartment> apartments;

    public Set<Apartment> getApartments() {
        return apartments;
    }

    public void setApartments(Set<Apartment> apartments) {
        this.apartments = apartments;
    }
}

I am wondering is it a bug in the Hibernate or I am doing something wrong. Relations like above used to work with the previously stated version of Hibernate.

Any pointer would be very helpful.

Regards,
Tapas

Hibernate ORM 6.1 is not supported anymore. Upgrade to 6.4.0.Final and retest please.

I would have used Hibernate ORM 6.4 if the TomEE 9.1 supports JPA 3.1, which is not the case.

I’m sorry for you, but if you’re relying on old software that requires special support for newer libraries but doesn’t receive that, then you will have to either stick to old library versions or consider switching to something like Quarkus.