I’m in the process of upgrading a project using Hibernate ORM from 5.3.14 to 6.1.3. I’ve got a SQLQuery that I’ve moved to NativeQuery, and am now getting the error:
jakarta.persistence.PersistenceException: Converting `org.hibernate.exception.SQLGrammarException` to JPA `PersistenceException` : Unable to find column position by name: title4_0_0_
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:165)
at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:374)
at org.hibernate.bugs.H2HibernateTest.testNativeQueryWithEntity(H2HibernateTest.java:83)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.hibernate.exception.SQLGrammarException: Unable to find column position by name: title4_0_0_
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:64)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:56)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.sql.results.jdbc.internal.AbstractResultSetAccess.resolveColumnPosition(AbstractResultSetAccess.java:67)
at org.hibernate.query.results.dynamic.DynamicFetchBuilderStandard.lambda$buildFetch$0(DynamicFetchBuilderStandard.java:94)
at org.hibernate.query.results.DomainResultCreationStateImpl.resolveSqlExpression(DomainResultCreationStateImpl.java:266)
at org.hibernate.query.results.dynamic.DynamicFetchBuilderStandard.lambda$buildFetch$1(DynamicFetchBuilderStandard.java:91)
at org.hibernate.metamodel.mapping.internal.BasicAttributeMapping.forEachSelectable(BasicAttributeMapping.java:363)
at org.hibernate.metamodel.mapping.ModelPart.forEachSelectable(ModelPart.java:93)
at org.hibernate.query.results.dynamic.DynamicFetchBuilderStandard.buildFetch(DynamicFetchBuilderStandard.java:108)
at org.hibernate.query.results.DomainResultCreationStateImpl.lambda$visitFetches$2(DomainResultCreationStateImpl.java:408)
The query is:
NativeQuery query = session.createNativeQuery("select {book.*} from zbook_t book");
The sql output by hibernate is below. Note the duplicated zpublish_3_0_0_:
select
book.ZBOOK_PK zbook_pk1_0_0_,
book.DESCRIPTION descript2_0_0_,
book.ZPUBLISHER_FK zpublish3_0_0_,
book.TITLE zpublish3_0_0_
from
zbook_t book
This appears to be due to the fact that we map the same column twice:
@ManyToOne(targetEntity = ZPublisher.class, fetch = FetchType.LAZY)
@JoinColumn(name = "ZPUBLISHER_FK")
private ZPublisher publisher;
@Column(name = "ZPUBLISHER_FK", nullable = false, insertable = false, updatable = false)
@Access(AccessType.FIELD)
private Long publisherFk;
If I either remove foreign key mappings, or remove the braces in the sql, which expands the attributes, then the problem goes away. I only understood that it was due to the braces expansion as I was writing this up. In our case, I think we can just remove the braces in the sql, since we only return one entity in the cases where we map the results to an entity. |