建议,我的表“类型”具有以下结构:
id | type | date
----------------------
1 | one | 2018-08-01
2 | two | 2018-08-03
3 | one | 2018-08-04
4 | two | 2018-08-05
我需要检索每种类型的最后一条记录(最后的日期)。我对此有一个本地查询:
SELECT id, type, max(date)
FROM types
GROUP BY type, id;
结果我有:
id | type | date
----------------------
3 | one | 2018-08-04
4 | two | 2018-08-05
我想将此查询转换为JPA查询语言。我怎样才能做到这一点?它比本地SQL查询好吗?如果没有,为什么?我想有可能在Spring存储库中像这样进行查询:
public interface TypeRepository extends JpaRepository<Type, Long> {
@Query(value="...")
List<Type> findLastTypes();
}
类型实体:
@Entity
@Table(name = "types")
public class Type extends Identifiable<Long> {
private String type;
private LocalDateTime date;
public Type() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
@Override
public Long getId() {
return super.id;
}
@Column(name = "type", nullable = false)
public String getType() {
return type;
}
@Column(name = "date", nullable = false)
public LocalDateTime getDate() {
return date;
}
// ... setters
}
谢谢前进