1

我试图用 Postman 运行 GettMapping。但它不起作用,我收到错误:

状态 500 错误。SQLGrammarException: 无法提取 ResultSet

   @GetMapping("/clients/month/{month}")
    public Meter getAllMeterByMonth(@PathVariable (value = "month") String month) {
        return meterRepository.findByMonth(month);
    }

存储库:

   public interface MeterRepository extends JpaRepository<Meter, Long> {

    Meter findByClientId(Long clientId);

    @Query(value = "select * from meter where month = :month", nativeQuery = true)
    Meter findByMonth(@Param("month")String month);

}

客户实体:

@Entity
@Table(name = "clients")

    public class Client {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotNull
    @Size(max = 100)
    private String name;

仪表实体:

@Entity
@Table(name = "meters")

    public class Meter{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Column(name="year")
    private int year;

    @NotNull
    @Column(name="month")
    private String month;

    @NotNull
    private int value;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "client_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    @JsonProperty("client_id")
    private Client client;

你对我的问题有什么想法吗?

4

1 回答 1

1

您只是因为一个简单的错字而面临这个错误。在MeterRepository.java中提到的查询中将meter替换为meter 。

像这样的东西:

package com.stackoverflow;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface MeterRepository extends JpaRepository<Meter, Long> {
    @Query(value = "select * from meters where month = :month", nativeQuery = true)
    Meter findByMonth(@Param("month")String month);
}
于 2018-11-23T00:58:36.283 回答