SQLGrammarException with native Query in spring boot












1














I tried to run a GettMapping with Postman. But it's not working and I am getting the error:



Status 500 error. SQLGrammarException: could not extract ResultSet



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


Repository:



   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);

}


Client Entity:



@Entity
@Table(name = "clients")

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


Meter entity:



@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;


Do you have any ideas about my issue ?










share|improve this question
























  • When yu get an exception, and you wonder why, you need to read the stack trace ofthe exception. And if you still don't understand, you need to post it. Guessing is hard. deducing from the message explaining the problem is much easier.
    – JB Nizet
    Nov 22 at 22:57






  • 1




    The message probably tells you that the table "meter" doesn't exist, since you mapped your entity to "meters". Why the hell are you using SQL rather than JPQL, BTW?
    – JB Nizet
    Nov 22 at 22:58










  • You have to check the log of your service rest to see what's the exacte error and not the stack that you have into postman. I have some doubt about your relation ManyToOne Could you add to your Client.java : @OneToMany(mappedBy = "client") private List<Metter> metters. Also you do not need to use Query juste findByMonth do the job
    – TinyOS
    Nov 22 at 23:42
















1














I tried to run a GettMapping with Postman. But it's not working and I am getting the error:



Status 500 error. SQLGrammarException: could not extract ResultSet



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


Repository:



   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);

}


Client Entity:



@Entity
@Table(name = "clients")

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


Meter entity:



@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;


Do you have any ideas about my issue ?










share|improve this question
























  • When yu get an exception, and you wonder why, you need to read the stack trace ofthe exception. And if you still don't understand, you need to post it. Guessing is hard. deducing from the message explaining the problem is much easier.
    – JB Nizet
    Nov 22 at 22:57






  • 1




    The message probably tells you that the table "meter" doesn't exist, since you mapped your entity to "meters". Why the hell are you using SQL rather than JPQL, BTW?
    – JB Nizet
    Nov 22 at 22:58










  • You have to check the log of your service rest to see what's the exacte error and not the stack that you have into postman. I have some doubt about your relation ManyToOne Could you add to your Client.java : @OneToMany(mappedBy = "client") private List<Metter> metters. Also you do not need to use Query juste findByMonth do the job
    – TinyOS
    Nov 22 at 23:42














1












1








1







I tried to run a GettMapping with Postman. But it's not working and I am getting the error:



Status 500 error. SQLGrammarException: could not extract ResultSet



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


Repository:



   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);

}


Client Entity:



@Entity
@Table(name = "clients")

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


Meter entity:



@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;


Do you have any ideas about my issue ?










share|improve this question















I tried to run a GettMapping with Postman. But it's not working and I am getting the error:



Status 500 error. SQLGrammarException: could not extract ResultSet



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


Repository:



   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);

}


Client Entity:



@Entity
@Table(name = "clients")

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


Meter entity:



@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;


Do you have any ideas about my issue ?







sql spring-boot spring-data-jpa get-mapping badsqlgrammarexception






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 14:37









Billy Frost

1,74698




1,74698










asked Nov 22 at 22:42









vapox

133




133












  • When yu get an exception, and you wonder why, you need to read the stack trace ofthe exception. And if you still don't understand, you need to post it. Guessing is hard. deducing from the message explaining the problem is much easier.
    – JB Nizet
    Nov 22 at 22:57






  • 1




    The message probably tells you that the table "meter" doesn't exist, since you mapped your entity to "meters". Why the hell are you using SQL rather than JPQL, BTW?
    – JB Nizet
    Nov 22 at 22:58










  • You have to check the log of your service rest to see what's the exacte error and not the stack that you have into postman. I have some doubt about your relation ManyToOne Could you add to your Client.java : @OneToMany(mappedBy = "client") private List<Metter> metters. Also you do not need to use Query juste findByMonth do the job
    – TinyOS
    Nov 22 at 23:42


















  • When yu get an exception, and you wonder why, you need to read the stack trace ofthe exception. And if you still don't understand, you need to post it. Guessing is hard. deducing from the message explaining the problem is much easier.
    – JB Nizet
    Nov 22 at 22:57






  • 1




    The message probably tells you that the table "meter" doesn't exist, since you mapped your entity to "meters". Why the hell are you using SQL rather than JPQL, BTW?
    – JB Nizet
    Nov 22 at 22:58










  • You have to check the log of your service rest to see what's the exacte error and not the stack that you have into postman. I have some doubt about your relation ManyToOne Could you add to your Client.java : @OneToMany(mappedBy = "client") private List<Metter> metters. Also you do not need to use Query juste findByMonth do the job
    – TinyOS
    Nov 22 at 23:42
















When yu get an exception, and you wonder why, you need to read the stack trace ofthe exception. And if you still don't understand, you need to post it. Guessing is hard. deducing from the message explaining the problem is much easier.
– JB Nizet
Nov 22 at 22:57




When yu get an exception, and you wonder why, you need to read the stack trace ofthe exception. And if you still don't understand, you need to post it. Guessing is hard. deducing from the message explaining the problem is much easier.
– JB Nizet
Nov 22 at 22:57




1




1




The message probably tells you that the table "meter" doesn't exist, since you mapped your entity to "meters". Why the hell are you using SQL rather than JPQL, BTW?
– JB Nizet
Nov 22 at 22:58




The message probably tells you that the table "meter" doesn't exist, since you mapped your entity to "meters". Why the hell are you using SQL rather than JPQL, BTW?
– JB Nizet
Nov 22 at 22:58












You have to check the log of your service rest to see what's the exacte error and not the stack that you have into postman. I have some doubt about your relation ManyToOne Could you add to your Client.java : @OneToMany(mappedBy = "client") private List<Metter> metters. Also you do not need to use Query juste findByMonth do the job
– TinyOS
Nov 22 at 23:42




You have to check the log of your service rest to see what's the exacte error and not the stack that you have into postman. I have some doubt about your relation ManyToOne Could you add to your Client.java : @OneToMany(mappedBy = "client") private List<Metter> metters. Also you do not need to use Query juste findByMonth do the job
– TinyOS
Nov 22 at 23:42












1 Answer
1






active

oldest

votes


















1














You are facing this error just because of a simple typo. Replace meter with meters in the query mentioned in MeterRepository.java.



Something like this:



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);
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438750%2fsqlgrammarexception-with-native-query-in-spring-boot%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You are facing this error just because of a simple typo. Replace meter with meters in the query mentioned in MeterRepository.java.



    Something like this:



    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);
    }





    share|improve this answer


























      1














      You are facing this error just because of a simple typo. Replace meter with meters in the query mentioned in MeterRepository.java.



      Something like this:



      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);
      }





      share|improve this answer
























        1












        1








        1






        You are facing this error just because of a simple typo. Replace meter with meters in the query mentioned in MeterRepository.java.



        Something like this:



        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);
        }





        share|improve this answer












        You are facing this error just because of a simple typo. Replace meter with meters in the query mentioned in MeterRepository.java.



        Something like this:



        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);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 0:58









        Kunal Puri

        1,684414




        1,684414






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438750%2fsqlgrammarexception-with-native-query-in-spring-boot%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks