I need to select all unique rows from Feed
table by reference_id
(ref_id
)
I wrote a request, but the request does not return unique values, but gives all the rows include duplicate rows with the same ref_id
.
So select doesn't work in right way
What is expected:
@Query("SELECT o FROM FeedEntity o WHERE o.referenceId IN (SELECT DISTINCT ol.referenceId FROM FeedEntity ol) AND o.id < :lastId")
List<FeedEntity> getByIdBefore(@Param("lastId") Long lastId, Pageable pageable);
This is the class that mapped for request:
public class FeedEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private UserEntity user;
@Enumerated
@Column(name = "type")
private FeedType type;
@Column(name = "ref_id")
private Long referenceId;
@Column(name = "create_time")
private Timestamp createTime;
public FeedEntity(UserEntity user, FeedType type, Long referenceId) {
this.user = user;
this.type = type;
this.referenceId = referenceId;
this.createTime = DateTimeUtil.getCurrentTimestamp();
}
}
Native query is working
SELECT * FROM feed
WHERE id IN (
SELECT DISTINCT MIN(Id)
FROM feed fe
GROUP BY fe.ref_id
)