需要查询并从hashMap返回对象

时间:2019-04-28 06:22:17

标签: spring jpa

我在Spring中有一个实体,该实体具有主键Timestamp和一个String,Double值的HashMap。

我的目标是使用时间戳查询并返回带有该时间戳的对象以及地图值。

我尝试了以下方法:

@Query("select s from Measurement s join s.map m where ?1 in (VALUE(m))")
Measurement findByTimestamp(ZonedDateTime timestamp);

实体:

@Entity
@Table(name="measurement")
@EnableAutoConfiguration
public class Measurement {


@Id
@JsonProperty(value="timestamp") @NotNull
private ZonedDateTime timestamp;

@JsonAnySetter
@ElementCollection
private Map<String, Double> metrics = new HashMap<>();
@ElementCollection
private Map<String, Double> metricsView;

public ZonedDateTime getTimestamp() {
  return timestamp;
}

Repository
@Repository
public interface MeasurementRepository extends JpaRepository<Measurement, Long> {

@Query("select s from Measurement s join s.map m where ?1 in (VALUE(m))")
Measurement findByTimestamp(ZonedDateTime timestamp);
}

期望如下:

{
  "dewPoint": 17.1,
  "precipitation": 0,
  "temperature": 27.5,
  "timestamp": "2015-09-01T16:20:00.000Z"
}

0 个答案:

没有答案