Is it possible to generate a native query with jooq and use hibernate to do the mapping? I am generating the exact same sql that hibernate shows with print-sql: true
, but am still getting org.hibernate.loader.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native-sql query
// first see what hibernate prints for the hql query
val hql = entityManager.createQuery("SELECT p FROM Parent p JOIN FETCH p.children WHERE p.id = ?1", Parent::class.java)
// shows
// select ce1_0.id, <other columns>, ce2_0.id, <other columns> from parent ce1_0 join children ce2_0 on ce1_0.id=ce2_0.parent_id where ce1_0.id=?
Now try and run the same query using a native query
// copy and paste exact query output that gets logged from HQL above
val query = em.createNativeQuery(copiedSql, Parent::class.java)
Running the above gives you the NonUniqueDiscoveredSqlAliasException
I originally tried this with jooq generation as well, but got the same error
val ce0 = Parents.PARENTS.`as`("ce1_0")
val ce1 = Children.CHILDREN.`as`("ce2_0")
val jooqQuery =
dsl.select(*ce0.fields(), *ce1.fields())
.from(ce0)
.join(ce1)
.on(ce0.ID.eq(ce1.ID))
.where(ce0.ID.eq(id))
// gives same error as above
val query = entityManager.createNativeQuery(jooqQuery.sql, Parent::class.java)