在 spring 引导中使用本机查询的 SQLGrammarException
SQLGrammarException with native Query in spring boot
我尝试 运行 使用 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;
你对我的问题有什么想法吗?
您只是因为一个简单的错字而遇到此错误。在 MeterRepository.java.
中提到的查询中将 meter 替换为 meters
像这样:
package com.Whosebug;
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);
}
我尝试 运行 使用 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;
你对我的问题有什么想法吗?
您只是因为一个简单的错字而遇到此错误。在 MeterRepository.java.
中提到的查询中将 meter 替换为 meters像这样:
package com.Whosebug;
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);
}