Hibernate returns last row n times

Hi!
I am using hibernate core 6.0.0.Alpha6, and mysql-connector 8.0.22. DB MySql org.hibernate.dialect.MySQLDialect. My problem occurs on previous release too.

Running a simple SQL query on a single table, no join, returns only last record for n times, where n is the correct records number.

HQL Query

 SELECT ig.id, ig.codice 
    FROM ImpiantiGestitiDto as ig 
    where ig.idPalestra='6' order by ig.id";

Schema: id=integer idPalestra=integer codice=varchar(6) ImpiantiGestitiDto=Dto

This is my code. I don’t use loop.

 Query<Object> query = session.createQuery(hql);
 result = query.getResultList(); 

The mysql-connector run the query succesfully:
Output on getResultList(); Wrong!

[[10,“000.03”],[10,“000.03”],[10,“000.03”],[10,“000.03”]]

I think the problem occurs in: -org.hibernate.sql.results.spi package; -class ListResultsConsumer; -method Consume.

in following loop, results.add(row) ; instructions occurs the error. Why occurs the problem? Can you help me? Thank you!

while (rowProcessingState.next()) {     
    final R row = rowReader.readRow( rowProcessingState, processingOptions );    
    boolean add = true;     
    if ( uniqueRows ) {         
       if ( results.contains( row ) ) { 
        add = false;        
       }
    }   
    if ( add ) {        
       results.add( row );  
    }   
    rowProcessingState.finishRowProcessing(); 
}

Using the query “FROM ImpiantiGestitiDto where idPalestra=‘6’ order by id” it’s works.

Using the query “SELECT id, codice FROM ImpiantiGestitiDto where idPalestra=‘6’ order by id” does not work.

I have to use the second query because the table ImpiantiGestitiDto is joined to another table and I have to use the WHERE clause.

Thanks for your answer.

I’m not sure I can follow. Can you show the entity mapping? If id or codice are not unique, you will obviously get multiple seemingly duplicate result rows.

Id is an autoincrement on my MYSQL DB
Codice has different value on records.
Using the query “FROM ImpiantiGestitiDto where idPalestra=‘6’ order by id” it’s works.

This is the ID entity class…
package com.etabeta.dto;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class IdDto {

@Id 
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY )  
@Column(name = "id", updatable = false, nullable = false)
private int id;	


public int getId() {
	return id;
}

public void setId(int id) {
	this.id = id;
}

}

This is a Codice Entity class…
package com.etabeta.dto;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class CodiceDto extends IdDto {

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

public String getCodice() {
	return codice;
}

public void setCodice(String codice) {
	this.codice = codice;
}	

}

Do you have a primary key constraint on the table?

Yes. Primary Btree Autoincrement

This might be an issue in Hibernate 6. As you might have noticed, Hibernate 6 is still in an Alpha state which means there might still be bugs. Please try using Hibernate 5.4. e.g.

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.4.23.Final</version>
</dependency>

Does not works.

JDBC returns this:
StandardRowReader - —Processing Row—
results - Extracted JDBC value [0] - [1]
results - Extracted JDBC value [1] - [000.00]
StandardRowReader - —Processing Row—
results - Extracted JDBC value [0] - [2]
results - Extracted JDBC value [1] - [000.01]
StandardRowReader - —Processing Row—
results - Extracted JDBC value [0] - [3]
results - Extracted JDBC value [1] - [000.02]
StandardRowReader - —Processing Row—
results - Extracted JDBC value [0] - [4]
results - Extracted JDBC value [1] - [000.03]

Hibernate returns this:
[[4,“000.03”],[4,“000.03”],[4,“000.03”],[4,“000.03”]]

Unless you are messing with the result somehow, this makes no sense. Could you please create a test case that reproduces this issue so that we can take a deeper look?

I send you my test code with configuration.

Thank you for your answer.
You can download from my drive the TestCase.zip.

-With IMPIANTI_GESTITI.sql you can import the table on mysql db.

-EntityMapping.zip contains the table hibernate mapping.
ImpiantiGestitiDto extends IdPalestraCodiceDto
IdPalestraCodiceDto extends CodiceDto
CodiceDto extends IdDto

-hibernate.cfg.xml is the hibernate configuration file

  • I run follow query:
    String hql = “SELECT id, codice FROM ImpiantiGestitiDto where idPalestra=‘6’ order by id”;
    Query query = session.createQuery(hql);
    result = query.list();
    query.list(); returns the last record for 4 times.

  • I run follow query:
    String hql = “FROM ImpiantiGestitiDto where idPalestra=‘6’ order by id”
    Query query = session.createQuery(hql);
    result = query.list();
    query.list(); returns the correct records

The attachments weren’t properly sent. Please fork the repository https://github.com/hibernate/hibernate-test-case-templates and create a test case with this template: https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java

You can then either send me the link to a pull request or to your fork so I can take a look.