Error advancing (next) ResultSet position + Hibernate v6

Hello Developers,

I recently upgraded hibernate to v6.1.6 from v5.4.33.

We have a table A and table B which is having one to many relation.
Also, the application is a legacy one so we are still using XML configuration and hbm.xml file.

My system.hbm.xml looks like this for these two tables

<class name="entity.TableA" table="table_a">
    <id name="id">
      <generator class="uuid"/>
    </id>
    <property name="CreatedDate" column="created_date"/>
    <property name="Name" column="name"/>
    <list name="Options" inverse="true" cascade="all-delete-orphan">
      <key column="value"/>
      <index column="seq_num"/>
      <one-to-many class="entity.TableB"/>
    </list>
  </class>
  <class name="entity.TableB" table="table_b">
    <id name="id">
      <generator class="uuid"/>
    </id>
    <property name="CreatedDate" column="created_date"/>
    <property name="SeqNum" column="seq_num"/>
    <property name="Value" column="value"/>
    <many-to-one name="Field" not-null="true" column="name"/>
  </class>

And I am trying to run a select query on the table_a like this

String hql = "from TableA"
Query query = session.createQuery(hql);
List found = query.list();

which is throwing this error.

Exception in thread "main" jakarta.persistence.PersistenceException: Converting `org.hibernate.sql.exec.ExecutionException` to JPA `PersistenceException` : A problem occurred in the SQL executor : Error advancing (next) ResultSet position
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:165)
	at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:374)
	at org.hibernate.query.sqm.internal.QuerySqmImpl.list(QuerySqmImpl.java:1073)
	at HibernateUtil.query(HibernateUtil.java:78)
	at HibernateUtil.findFields(HibernateUtil.java:126)
	at HibernateImpl.main(HibernateImpl.java:11)
Caused by: org.hibernate.sql.exec.ExecutionException: A problem occurred in the SQL executor : Error advancing (next) ResultSet position
	at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.makeExecutionException(JdbcValuesResultSetImpl.java:251)
	at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.advanceNext(JdbcValuesResultSetImpl.java:208)
	at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.processNext(JdbcValuesResultSetImpl.java:85)
	at org.hibernate.sql.results.jdbc.internal.AbstractJdbcValues.next(AbstractJdbcValues.java:29)
	at org.hibernate.sql.results.internal.RowProcessingStateStandardImpl.next(RowProcessingStateStandardImpl.java:88)
	at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:197)
	at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33)
	at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:443)
	at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:166)
	at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:91)
	at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31)
	at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.lambda$new$0(ConcreteSqmSelectQueryPlan.java:113)
	at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:335)
	at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:276)
	at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:571)
	at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:363)
	... 4 more
Caused by: org.hibernate.exception.GenericJDBCException: Error advancing (next) ResultSet position
	at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:61)
	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
	... 20 more
Caused by: java.sql.SQLException: Closed Resultset: next

I need to make it working with hibernate v6.1.6.Final. Please guide me in resolving this, or if I am missing any configuration or something.
NOTE: This code works with Hibernate v5.6.14.Final without any error.

Can you check what SQL is sent to the database since it is an SQLException?

@mkomko it run three queries

Hibernate: select m1_0.id,m1_0.created_date,m1_0.name from table_a m1_0
Hibernate: select o1_0.id,o1_0.seq_num,o1_0.created_date,o1_0.value from table_b o1_0
Hibernate: select o1_0.id,o1_0.seq_num,o1_0.created_date,o1_0.value from table_b o1_0

Hi @ankit_agrahari,

can you provide a simple test that reproduce your issue? this would be really helpful

Thanks

@dreab8 You can refer to this sample project Hibernatev6, which will throw the error if the hibernate version is 6.1.6. But works if we change the version to 5.6.14 in pom.xml.

You can add the DB details in the hibernate.properties file present under the resource dir.

thanks @ankit_agrahari , I’m going to give a look at it!

1 Like

Hi @ankit_agrahari , trying your test but I cannot face any issue, probably because it does not populate the database at all and so it just executes a from MetadataField f query on an empty db.

Hi @dreab8,

Thanks for trying out.

Yes, If the data is not present, it would work without any error, but if the table has some data, then it it giving that error.

Hi @ankit_agrahari ,

can you please open a Jira ?

Thanks.

FYI the issue is triggered by the iteration of the collection inside the setOptions(List<MetadataOption> options) method that is by the way a bad practice when you are using a property-based access strategy.

So at the moment you have few options, remove the iteration for the setOptions(List<MetadataOption> options) method or change Hibernate access strategy to field-based or enclose your query inside a transaction.

Thanks @dreab8
I have raised one issue HHH-16120

Regards

Thanks @ankit_agrahari !

I’m going to work on it.

Regards

1 Like

Hi @dreab8,

Thank you for your efforts, I see the PR is merged.
So, I wanted to know when will this be available ?

Regards

Hi @ankit_agrahari ,

we are going to release 6.1.7.Final today.

Regards

1 Like

Hi @dreab8,

I was testing the code with the build 6.1.7.Final and it is working now.
But I observed one thing is that, if the table_A has some 50 records, and we are querying without any where constraint like select * from table_A, it is executing 100 queries in the background. This is degrading the performance.

Let me know your thoughts on it and if there is an option to fix it.

Regards.

it sounds like a bug but difficult to say with just the information you provided.
Can you please provide a reproducer?

Thanks

Its the same code base Hibernatev6, I just added show_sql as true and it showed twice the queries running to the number of records present in the table.

it seems this happens only when MetadataField has no MetadataOption, can you please open a Jira? Thanks

One suggestion, it is not good a practice to add logic into the setter methods when you use a property-based access strategy.

Hi @ankit_agrahari I have created this Jira [HHH-16184] - Hibernate JIRA

Hi @dreab8 , Thanks a lot for creating the JIRA, I was away last week.
And regarding the logic in setter methods, we have some legacy code which is present as is and we are planning to upgrade in future, but not as of now.

Thanks.

Thank you for your efforts, I see the PR is merged.

1 Like