EN
JPA / Hibernate - returns duplicated rows
1 answers
4 points
I have an issue: Hibernate returns duplicated rows in the list of entities.
I use a native query with GROUP BY:
xxxxxxxxxx
1
SELECT
2
COUNT(`name`) AS `count`,
3
`name`,
4
`type`,
5
MIN(`creation_time`) as `creation_time`,
6
MAX(`update_time`) as `update_time`
7
FROM `documents` p
8
WHERE
9
`type` = 'word'
10
AND `removal_time` IS NULL
11
GROUP BY `name`, `type`
12
ORDER BY `id` DESC
13
LIMIT 0, 20
Duplicated rows:

1 answer
2 points
Probably you have defined @Id
in the wrong way in your entity.
You need to set @Id
for name
an type
.
Try:
DocumentEntity.java
file:
xxxxxxxxxx
1
package example.database.entities;
2
3
import lombok.Getter;
4
import lombok.Setter;
5
6
import javax.persistence.*;
7
import java.util.Date;
8
9
10
11
12
DocumentKey.class) // <------------ REQUIRED (
13
public class DocumentEntity {
14
15
private Long count;
16
17
// <-------------------------------- REQUIRED
18
private String name;
19
20
// <-------------------------------- REQUIRED
21
private String type;
22
23
private Date creationTime;
24
private Date updateTime;
25
}
DocumentKey.java
file:
xxxxxxxxxx
1
package example.database.entities;
2
3
import lombok.*;
4
5
import java.io.Serializable;
6
import java.util.Objects;
7
8
9
10
11
12
public class DocumentKey implements Serializable {
13
14
private String name;
15
private String type;
16
17
18
public boolean equals(Object o) {
19
if (this == o) {
20
return true;
21
}
22
if (o == null || getClass() != o.getClass()) {
23
return false;
24
}
25
DocumentKey that = (DocumentKey) o;
26
return Objects.equals(this.name, that.name) && Objects.equals(this.type, that.type);
27
}
28
29
30
public int hashCode() {
31
return Objects.hash(this.name, this.type);
32
}
33
}
0 commentsShow commentsAdd comment