运行MySQL查询时出现问题。

huangapple go评论95阅读模式
英文:

issue in running the mysql query

问题

  1. @Query(value="select sqla.questionBO from StudBO sqla where sqa.questionBO.id != null and sqa.lessionId = :lessionId and sqa.studentBO.id=:studentId ORDER BY sqa.attemptedOn DESC limit 1", nativeQuery=true)
  2. List<QuestionBO> getSQues(@Param("studentId") Long studentId, @Param("lessionId") Long lessionId);
  3. // ...
  4. @Table(name = "student_question_attempt")
  5. public class StudQuesAttemptBO implements Serializable {
  6. @Column(name = "ATTEMPTED_ON")
  7. private Timestamp attemptedOn;
  8. //@Column(name = "STUDENT_ID")
  9. @ManyToOne
  10. @JoinColumn(name = "STUDENT_ID")
  11. private StudentBO studentBO;
  12. // @Column(name = "QUESTION_ID")
  13. @ManyToOne(fetch = FetchType.LAZY)
  14. @JoinColumn(name = "QUESTION_ID")
  15. private QuestionBO questionBO;
  16. @Column(name = "SCORE")
  17. private Double score;
  18. @Column(name = "LESSION_ID")
  19. private Long lessionId;
  20. }
  21. // ...
  22. if i use table name instead of BO then getting:-
  23. ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'sqla.questionBO' in 'field list'
英文:

Query: -

  1. @Query(value=&quot;select sqla.questionBO from StudBO sqla where sqa.questionBO.id != null and sqa.lessionId = :lessionId and sqa.studentBO.id=:studentId ORDER BY sqa.attemptedOn DESC limit 1&quot;, nativeQuery=true)
  2. List&lt;QuestionBO&gt; getSQues(@Param(&quot;studentId&quot;) Long studentId,
  3. @Param(&quot;lessionId&quot;) Long lessionId);

I'm upgrading my MySQL from v5 to v8 earlier I was not using nativeQuery = true variable and it was working fine, but I think now I have to use native Query as I'm using the limit function.

If I don't use nativeQuery then I get "antlr.NoViableAltException: unexpected token: limit"
error at tomcat startup.

But when I use nativeQuery I get the table not found error "ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Table &#39;latest.studquesattemptbo&#39; doesn&#39;t exist" when this method is called.

I checked my.ini file and there "lower_case_table_names=1" is already present as pointed in this link "https://knowledge.broadcom.com/external/article/47334/enterprise-dashboard-error-sqlexceptionh.html"

Edit1:-
its BO.

  1. @Table(name = &quot;student_question_attempt&quot;)
  2. public class StudQuesAttemptBO implements Serializable {
  3. @Column(name = &quot;ATTEMPTED_ON&quot;)
  4. private Timestamp attemptedOn;
  5. //@Column(name = &quot;STUDENT_ID&quot;)
  6. @ManyToOne
  7. @JoinColumn(name = &quot;STUDENT_ID&quot;)
  8. private StudentBO studentBO;
  9. // @Column(name = &quot;QUESTION_ID&quot;)
  10. @ManyToOne(fetch = FetchType.LAZY)
  11. @JoinColumn(name = &quot;QUESTION_ID&quot;)
  12. private QuestionBO questionBO;
  13. @Column(name = &quot;SCORE&quot;)
  14. private Double score;
  15. @Column(name = &quot;LESSION_ID&quot;)
  16. private Long lessionId;

Edit2:-

if i use table name instead of BO then getting:-

  1. ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column &#39;sqla.questionBO&#39; in &#39;field list&#39;

答案1

得分: 1

使用“Pageable firstRecord = PageRequest.of(0, 1);”而不是更改查询为本机查询,并将其传递给方法调用的参数。

英文:

instead of changing the query to native query use "Pageable firstRecord = PageRequest.of(0, 1);" and pass this in the parameter of the method call

huangapple
  • 本文由 发表于 2023年6月22日 20:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定