【问题标题】:Spring Boot native query throws error, but works in mysql [duplicate]Spring Boot本机查询引发错误,但在mysql中有效[重复]
【发布时间】:2020-08-05 04:10:02
【问题描述】:

我的实际 MySQL 查询是:

select * from (select * from consultations where doctor_id='9188389277441234567890' and is_deleted = 0 ORDER BY updated_at desc) as c GROUP BY patient_id, doctor_id, diagnosis_id;

我喜欢在 Spring Boot JPA 中执行相同的查询。我的回购代码是:

@Query(value = "select c from (select cd from consultations cd where cd.doctor_id=?1 and cd.is_deleted = 0 ORDER BY cd.updated_at desc) as c GROUP BY c.patient_id, c.doctor_id, c.diagnosis_id", nativeQuery = true)
public Page<Consultations> findForDoctor(@Param("username") String username, Pageable pageable);

我得到了

  "message": "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"

请告诉我如何解决这个问题?

提前感谢答案,我尝试了这么久。

按照评论中的要求添加模型详细信息:

import java.io.Serializable;
import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

@Entity
@Table(name = "consultations")
@Data
public class Consultations implements Serializable {

    private static final long serialVersionUID = 6576324953564602096L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "doctor_id")
    private String doctorId;

    @Column(name = "patient_id")
    private String patientId;

    @Column(name = "diagnosis_id")
    private Long diagnosisId;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @Column(name = "is_deleted")
    private Boolean isDeleted = false;

}

这是我得到的错误:

2020-04-22 14:30:00,977 DEBUG org.hibernate.SQL : select count(* from (select *) from consultations where doctor_id=? and is_deleted = 0
Hibernate: select count(* from (select *) from consultations where doctor_id=? and is_deleted = 0
2020-04-22 14:30:01,321 DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper : could not extract ResultSet [n/a]
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from (select *) from consultations where doctor_id='9188389277441234567890' a' at line 1

【问题讨论】:

  • 你扩展接口JpaRepository&lt;&gt;吗?
  • 是的。公共接口 ConsultationsRepo 扩展 JpaRepository {}
  • 好的,您可以编辑帖子并添加咨询模型(实体)代码吗?我想我可以解决你的问题。
  • 如您所说@Seldo97 在问题中编辑
  • 这能回答你的问题吗? Spring Data and Native Query with pagination

标签: java mysql hibernate spring-boot spring-data-jpa


【解决方案1】:

非常感谢您的帮助。

特别感谢@Lemmy。有帮助。

我像这样更改了我的代码。

@Query(value = "select * from (select * from consultations where doctor_id=?1 and is_deleted = 0 ORDER BY updated_at desc) as c GROUP BY patient_id, doctor_id, diagnosis_id", 
            countQuery = "select count(*) from (select * from consultations where doctor_id=?1 and is_deleted = 0 ORDER BY updated_at desc) as c GROUP BY patient_id, doctor_id, diagnosis_id",
            nativeQuery = true)

现在可以了。

【讨论】:

    猜你喜欢
    • 2015-10-26
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2018-03-05
    相关资源
    最近更新 更多