After Hibernate 6 upgrade native query on parent class fails with “ORA-17006: Invalid column name”

Hi,

After upgrading from Hibernate 5 to Hibernate 6, a native query that used to work now fails with an invalid column name error.

Entity definitions

Base class:

@Entity
@Table(name = "POINTS")
@Inheritance(strategy = InheritanceType.JOINED)
public class Point {

    @Id
    private Long id;

    @Column(name = "POINT_NAME")
    private String name;

    @Size(max = 250)
    @Column(name = "DESCRIPTION")
    private String description;
    
    // more columns...
}

Subclass:

@Entity
@Table(name = "REFERENTIALS")
@PrimaryKeyJoinColumn(name = "REFERENTIAL_ID")
public class Referential extends Point {

    @NotNull
    @Size(max = 30)
    @Column(name = "REFERENTIAL_NAME")
    private String referentialName;
}

The query

I have a mechanism that dynamically builds a native SQL query, and it produces:

SELECT points_0.*, 0 as clazz_
FROM points points_0

Which is executed with the entity manager :

entityManager.createNativeQuery("SELECT points_0.*, 0 as clazz_ FROM points points_0", Point.class);

This query worked fine with Hibernate 5.

The problem

After upgrading to Hibernate 6, it now fails with the following error:

DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - Unable to find column position by name: referential_name [n/a]
java.sql.SQLException: ORA-17006: Invalid column name

It looks like Hibernate 6 tries to map subclass columns (referential_name) even though the query only targets the base table (POINTS).

How can I execute a native query that maps only to the base class (Point) in Hibernate 6 without Hibernate trying to include subclass columns?

thank you !

Hello @accelerating_bear, many aspects of queries have changed from H5 to H6, and the latter now expects all columns of the entity hierarchy you’re selecting to be present. If you feel like this is a bug, feel free to create an issue in the issue tracker with a test case that reproduces the issue.

I’m wondering though, why is it you’re using a native query when you need to retrieve the entire entity class anyway? With HQL you could very easily write something like:

select p from Point p where type(p) = Point

And only get entities of the super-type.

I assume you created the HHH-19881 Jira issue? Like I wrote in the comment, this is expected and also documented behavior.