Error In Native Query where column is mapped twice

I’m in the process of upgrading a project using Hibernate ORM from 5.3.14 to 6.1.3. I’ve got a number of SQLQuery that I’ve moved to NativeQuery, and am now getting the error:

jakarta.persistence.PersistenceException: Converting org.hibernate.exception.GenericJDBCExceptionto JPAPersistenceException : Unable to find column position by name: user_id27_190_0_

            NativeQuery query = getSession().createNativeQuery(sql);
            query.addEntity("job", TMDBJob.class);
            results = query.list();

The SQL starts with

"select /*+ index(job IDX_JOB_RUNNABLE) */ {job.*} from Job_t  job where ..."

where the comment /* …*/ is just there to identify it in the logs. When I look at the actual SQL in those logs, I see (after pretty printing), the following. Note the duplicated aliases for job_group_fk8_190_0_, causing subsequent columns to have aliases that are off by 1. The same goes for study_fk24_190_0_:

SELECT /*+ index(job IDX_JOB_RUNNABLE) */
    job.job_pk                         job_pk1_190_0_,
    job.completed_units                completed_units2_190_0_,
    job.job_group_fk                   job_group_fk8_190_0_,
    job.mgr_id                         job_group_fk8_190_0_,
    job.job_state                      mgr_id9_190_0_,
    job.total_units                    study_fk24_190_0_,
    job.update_sequence                study_fk24_190_0_,
    job.user_id                        total_units25_190_0_,
    job.worker                         update_sequence26_190_0_

So, when it comes around to user_id, it does not find any column by that name.

I suspect this is because we have a couple of unusual mapping in our Job Entity, where both an entity and its foreign key are mapped to the same column, so that we can use the FK without having to first fetch the entity just to get its PK, Here’s the one for study:

@ManyToOne(targetEntity = TMDBStudy.class, fetch = FetchType.LAZY)
@JoinColumn(name = "STUDY_FK")
private TMStudy study;

@Column(name = "STUDY_FK", nullable = false, insertable = false, updatable = false)
private Long studyFk;

The other is for job_group.

This works without issue in 5.3.24 and has for some time.
Is this a bug? And/or is there a better way to achieve this, now?


Yeah, this looks like a bug. Please create an issue in the issue tracker( with a test case(hibernate-test-case-templates/ at main · hibernate/hibernate-test-case-templates · GitHub) that reproduces the issue.

Sorry, I forgot to note here that I created the issue, [HHH-15608] - Hibernate JIRA.

1 Like