# Hibernate returns last row n times

**URL:** <https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720>\
**Category:** Hibernate ORM\
**Created:** [November 4, 2020, 10:05am UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720 "2020-11-04T10:05:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![filippodb](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@filippodb](https://discourse.hibernate.org/u/filippodb)\
**Post date:** [November 4, 2020, 10:05am UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/1 "2020-11-04T10:05:25Z")

</div>

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

```auto
 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.

```auto
 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!

```auto
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.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 4, 2020, 4:40pm UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/2 "2020-11-04T16:40:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![filippodb](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@filippodb](https://discourse.hibernate.org/u/filippodb)\
**Post date:** [November 4, 2020, 5:01pm UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/3 "2020-11-04T17:01:20Z")

</div>

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;
}	

```

}

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 4, 2020, 8:18pm UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/4 "2020-11-04T20:18:42Z")

</div>

Do you have a primary key constraint on the table?

---

<div class="post-metadata">

**Author:** ![filippodb](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@filippodb](https://discourse.hibernate.org/u/filippodb)\
**Post date:** [November 4, 2020, 8:32pm UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/5 "2020-11-04T20:32:16Z")

</div>

Yes. Primary Btree Autoincrement

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 4, 2020, 8:47pm UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/6 "2020-11-04T20:47:27Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![filippodb](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@filippodb](https://discourse.hibernate.org/u/filippodb)\
**Post date:** [November 4, 2020, 9:26pm UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/7 "2020-11-04T21:26:35Z")

</div>

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”]]

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 5, 2020, 6:41am UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/8 "2020-11-05T06:41:24Z")

</div>

Unless you are messing with the result somehow, this makes no sense. Could you please create a [test case](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) that reproduces this issue so that we can take a deeper look?

---

<div class="post-metadata">

**Author:** ![filippodb](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@filippodb](https://discourse.hibernate.org/u/filippodb)\
**Post date:** [November 5, 2020, 9:15am UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/9 "2020-11-05T09:15:40Z")

</div>

I send you my test code with configuration.

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

> **[Meet Google Drive – One place for all your files](https://accounts.google.com/ServiceLogin?service=wise&passive=1209600&continue=https:%2F%2Fdrive.google.com%2Ffile%2Fd%2F1Aa5gYNppJUWQKNbMOjMIbyacYU59ow1U%2Fview?usp%3Dsharing&followup=https:%2F%2Fdrive.google.com%2Ffile%2Fd%2F1Aa5gYNppJUWQKNbMOjMIbyacYU59ow1U%2Fview?usp%3Dsharing)**

-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

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 5, 2020, 9:33am UTC](https://discourse.hibernate.org/t/hibernate-returns-last-row-n-times/4720/10 "2020-11-05T09:33:10Z")

</div>

The attachments weren’t properly sent. Please fork the repository [https://github.com/hibernate/hibernate-test-case-templates](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](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.
