运行MySQL查询时出现问题。

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

issue in running the mysql query

问题

@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)
List<QuestionBO> getSQues(@Param("studentId") Long studentId, @Param("lessionId") Long lessionId);

// ...

@Table(name = "student_question_attempt")
public class StudQuesAttemptBO implements Serializable {
    @Column(name = "ATTEMPTED_ON")
    private Timestamp attemptedOn;
    
    //@Column(name = "STUDENT_ID")
    @ManyToOne
    @JoinColumn(name = "STUDENT_ID")
    private StudentBO studentBO;
    
    // @Column(name = "QUESTION_ID")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "QUESTION_ID")
    private QuestionBO questionBO;
    
    @Column(name = "SCORE")
    private Double score;
    
    @Column(name = "LESSION_ID")
    private Long lessionId;
}

// ...

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

ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'sqla.questionBO' in 'field list'
英文:

Query: -

@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)
	List&lt;QuestionBO&gt; getSQues(@Param(&quot;studentId&quot;) Long studentId,
			@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.

 @Table(name = &quot;student_question_attempt&quot;)
    public class StudQuesAttemptBO implements Serializable {
@Column(name = &quot;ATTEMPTED_ON&quot;)
    private Timestamp attemptedOn;

    //@Column(name = &quot;STUDENT_ID&quot;)
    @ManyToOne
    @JoinColumn(name = &quot;STUDENT_ID&quot;)
    private StudentBO studentBO;

   // @Column(name = &quot;QUESTION_ID&quot;)
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = &quot;QUESTION_ID&quot;)
    private QuestionBO questionBO;
    
    @Column(name = &quot;SCORE&quot;)
    private Double score;
    
    @Column(name = &quot;LESSION_ID&quot;)
    private Long lessionId;

Edit2:-

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

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:

确定