@Query annotation with custom Class Spring Boot Java [closed]












0















question for JPA-Users, is the following somehow possible?



@Query(value = "SELECT * FROM Users u WHERE u.status = :sampleClass.status", 
nativeQuery = true)
List<SampleClass> findBySampleClass(SampleClass sampleClass);


Notice the way I am trying to access SampleClass in the @Query annotation. Wasn't able to get this going, instead went for Criteria and constructed my query old-school.










share|improve this question















closed as off-topic by Alan Hay, cнŝdk, Nic3500, Matthieu Brucher, blue-phoenox Nov 19 '18 at 17:59


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Alan Hay, Nic3500, Matthieu Brucher, blue-phoenox

If this question can be reworded to fit the rules in the help center, please edit the question.














  • To clarify, so you want to pass the whole object to query, not the status alone?

    – Clomez
    Nov 19 '18 at 12:43











  • :sampleClass.status - is the problem..

    – Lova Chittumuri
    Nov 19 '18 at 12:47











  • Please provide exact value as :status from calling method.

    – Raheela Aslam
    Nov 19 '18 at 12:48
















0















question for JPA-Users, is the following somehow possible?



@Query(value = "SELECT * FROM Users u WHERE u.status = :sampleClass.status", 
nativeQuery = true)
List<SampleClass> findBySampleClass(SampleClass sampleClass);


Notice the way I am trying to access SampleClass in the @Query annotation. Wasn't able to get this going, instead went for Criteria and constructed my query old-school.










share|improve this question















closed as off-topic by Alan Hay, cнŝdk, Nic3500, Matthieu Brucher, blue-phoenox Nov 19 '18 at 17:59


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Alan Hay, Nic3500, Matthieu Brucher, blue-phoenox

If this question can be reworded to fit the rules in the help center, please edit the question.














  • To clarify, so you want to pass the whole object to query, not the status alone?

    – Clomez
    Nov 19 '18 at 12:43











  • :sampleClass.status - is the problem..

    – Lova Chittumuri
    Nov 19 '18 at 12:47











  • Please provide exact value as :status from calling method.

    – Raheela Aslam
    Nov 19 '18 at 12:48














0












0








0








question for JPA-Users, is the following somehow possible?



@Query(value = "SELECT * FROM Users u WHERE u.status = :sampleClass.status", 
nativeQuery = true)
List<SampleClass> findBySampleClass(SampleClass sampleClass);


Notice the way I am trying to access SampleClass in the @Query annotation. Wasn't able to get this going, instead went for Criteria and constructed my query old-school.










share|improve this question
















question for JPA-Users, is the following somehow possible?



@Query(value = "SELECT * FROM Users u WHERE u.status = :sampleClass.status", 
nativeQuery = true)
List<SampleClass> findBySampleClass(SampleClass sampleClass);


Notice the way I am trying to access SampleClass in the @Query annotation. Wasn't able to get this going, instead went for Criteria and constructed my query old-school.







java sql spring spring-boot spring-data-jpa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 14:57









Billy Frost

1,74598




1,74598










asked Nov 19 '18 at 12:41









Thomas NowickiThomas Nowicki

113




113




closed as off-topic by Alan Hay, cнŝdk, Nic3500, Matthieu Brucher, blue-phoenox Nov 19 '18 at 17:59


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Alan Hay, Nic3500, Matthieu Brucher, blue-phoenox

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Alan Hay, cнŝdk, Nic3500, Matthieu Brucher, blue-phoenox Nov 19 '18 at 17:59


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Alan Hay, Nic3500, Matthieu Brucher, blue-phoenox

If this question can be reworded to fit the rules in the help center, please edit the question.













  • To clarify, so you want to pass the whole object to query, not the status alone?

    – Clomez
    Nov 19 '18 at 12:43











  • :sampleClass.status - is the problem..

    – Lova Chittumuri
    Nov 19 '18 at 12:47











  • Please provide exact value as :status from calling method.

    – Raheela Aslam
    Nov 19 '18 at 12:48



















  • To clarify, so you want to pass the whole object to query, not the status alone?

    – Clomez
    Nov 19 '18 at 12:43











  • :sampleClass.status - is the problem..

    – Lova Chittumuri
    Nov 19 '18 at 12:47











  • Please provide exact value as :status from calling method.

    – Raheela Aslam
    Nov 19 '18 at 12:48

















To clarify, so you want to pass the whole object to query, not the status alone?

– Clomez
Nov 19 '18 at 12:43





To clarify, so you want to pass the whole object to query, not the status alone?

– Clomez
Nov 19 '18 at 12:43













:sampleClass.status - is the problem..

– Lova Chittumuri
Nov 19 '18 at 12:47





:sampleClass.status - is the problem..

– Lova Chittumuri
Nov 19 '18 at 12:47













Please provide exact value as :status from calling method.

– Raheela Aslam
Nov 19 '18 at 12:48





Please provide exact value as :status from calling method.

– Raheela Aslam
Nov 19 '18 at 12:48












3 Answers
3






active

oldest

votes


















1














There should not be any space in between = and : so change the query to below one.



SELECT * FROM Users u WHERE u.status =: sampleClass.status


Also as your mathod name findBySampleClass, you are trying to find based on SampleClass then why only passing one parameter of SampleClass instead of object??



see this for much clarity.



You can use like below also by indexing parameters like ?1.



@Query(value = "select id,name,roll_no from USER_INFO_TEST where rollNo = ?1", nativeQuery = true)
ArrayList<IUserProjection> findUserUsingRollNo(String rollNo);


Refer : Quote from Spring Data JPA reference docs.



Also, see this section on how to do it with a named native query.






share|improve this answer

































    1














    What you are trying to do is not possible because in native queries you have support just for binding direct variables, you can not do fancy access on objects. You could do something similar to your attempt by using either JPQL or HQL (in case you are using hibernate as an ORM provider).



    But there is a problem on higher level. You are calling your method findBySampleClass, you are passing SampleClass as an input parameter and expect SampleClass as an output.This means that you would be returning the object itself from semantic point of view. This is suspicious. But going further on, you are doing something different inside the actual query specfication; you are using the status property of the object you passed in. This deviates from the convention that the method name should say what the query will be. In this case the correct(and most natural) way to go is to pass the status as a parameter, rename the method to findBySampleClassStatus or findByStatus.






    share|improve this answer

































      1














      You could use the approach as mentioned in the JPA docs:



          @Query(value = "SELECT * FROM Users u WHERE u.status = ?1",
      nativeQuery = true)
      List<SampleClass> findBySampleClass(String status);


      Please refer to the official docs for further options : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_native_queries



      Not sure why you are trying to pass the whole object though.Would you care to explain?






      share|improve this answer





















      • 1





        While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

        – Mihai Chelaru
        Nov 19 '18 at 14:05











      • @MihaiChelaru - Have updated the answer as suggested. Will keep the suggestion in mind for future answers.

        – user3392782
        Nov 19 '18 at 14:51


















      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      There should not be any space in between = and : so change the query to below one.



      SELECT * FROM Users u WHERE u.status =: sampleClass.status


      Also as your mathod name findBySampleClass, you are trying to find based on SampleClass then why only passing one parameter of SampleClass instead of object??



      see this for much clarity.



      You can use like below also by indexing parameters like ?1.



      @Query(value = "select id,name,roll_no from USER_INFO_TEST where rollNo = ?1", nativeQuery = true)
      ArrayList<IUserProjection> findUserUsingRollNo(String rollNo);


      Refer : Quote from Spring Data JPA reference docs.



      Also, see this section on how to do it with a named native query.






      share|improve this answer






























        1














        There should not be any space in between = and : so change the query to below one.



        SELECT * FROM Users u WHERE u.status =: sampleClass.status


        Also as your mathod name findBySampleClass, you are trying to find based on SampleClass then why only passing one parameter of SampleClass instead of object??



        see this for much clarity.



        You can use like below also by indexing parameters like ?1.



        @Query(value = "select id,name,roll_no from USER_INFO_TEST where rollNo = ?1", nativeQuery = true)
        ArrayList<IUserProjection> findUserUsingRollNo(String rollNo);


        Refer : Quote from Spring Data JPA reference docs.



        Also, see this section on how to do it with a named native query.






        share|improve this answer




























          1












          1








          1







          There should not be any space in between = and : so change the query to below one.



          SELECT * FROM Users u WHERE u.status =: sampleClass.status


          Also as your mathod name findBySampleClass, you are trying to find based on SampleClass then why only passing one parameter of SampleClass instead of object??



          see this for much clarity.



          You can use like below also by indexing parameters like ?1.



          @Query(value = "select id,name,roll_no from USER_INFO_TEST where rollNo = ?1", nativeQuery = true)
          ArrayList<IUserProjection> findUserUsingRollNo(String rollNo);


          Refer : Quote from Spring Data JPA reference docs.



          Also, see this section on how to do it with a named native query.






          share|improve this answer















          There should not be any space in between = and : so change the query to below one.



          SELECT * FROM Users u WHERE u.status =: sampleClass.status


          Also as your mathod name findBySampleClass, you are trying to find based on SampleClass then why only passing one parameter of SampleClass instead of object??



          see this for much clarity.



          You can use like below also by indexing parameters like ?1.



          @Query(value = "select id,name,roll_no from USER_INFO_TEST where rollNo = ?1", nativeQuery = true)
          ArrayList<IUserProjection> findUserUsingRollNo(String rollNo);


          Refer : Quote from Spring Data JPA reference docs.



          Also, see this section on how to do it with a named native query.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 13:21

























          answered Nov 19 '18 at 13:14









          AlienAlien

          4,85331026




          4,85331026

























              1














              What you are trying to do is not possible because in native queries you have support just for binding direct variables, you can not do fancy access on objects. You could do something similar to your attempt by using either JPQL or HQL (in case you are using hibernate as an ORM provider).



              But there is a problem on higher level. You are calling your method findBySampleClass, you are passing SampleClass as an input parameter and expect SampleClass as an output.This means that you would be returning the object itself from semantic point of view. This is suspicious. But going further on, you are doing something different inside the actual query specfication; you are using the status property of the object you passed in. This deviates from the convention that the method name should say what the query will be. In this case the correct(and most natural) way to go is to pass the status as a parameter, rename the method to findBySampleClassStatus or findByStatus.






              share|improve this answer






























                1














                What you are trying to do is not possible because in native queries you have support just for binding direct variables, you can not do fancy access on objects. You could do something similar to your attempt by using either JPQL or HQL (in case you are using hibernate as an ORM provider).



                But there is a problem on higher level. You are calling your method findBySampleClass, you are passing SampleClass as an input parameter and expect SampleClass as an output.This means that you would be returning the object itself from semantic point of view. This is suspicious. But going further on, you are doing something different inside the actual query specfication; you are using the status property of the object you passed in. This deviates from the convention that the method name should say what the query will be. In this case the correct(and most natural) way to go is to pass the status as a parameter, rename the method to findBySampleClassStatus or findByStatus.






                share|improve this answer




























                  1












                  1








                  1







                  What you are trying to do is not possible because in native queries you have support just for binding direct variables, you can not do fancy access on objects. You could do something similar to your attempt by using either JPQL or HQL (in case you are using hibernate as an ORM provider).



                  But there is a problem on higher level. You are calling your method findBySampleClass, you are passing SampleClass as an input parameter and expect SampleClass as an output.This means that you would be returning the object itself from semantic point of view. This is suspicious. But going further on, you are doing something different inside the actual query specfication; you are using the status property of the object you passed in. This deviates from the convention that the method name should say what the query will be. In this case the correct(and most natural) way to go is to pass the status as a parameter, rename the method to findBySampleClassStatus or findByStatus.






                  share|improve this answer















                  What you are trying to do is not possible because in native queries you have support just for binding direct variables, you can not do fancy access on objects. You could do something similar to your attempt by using either JPQL or HQL (in case you are using hibernate as an ORM provider).



                  But there is a problem on higher level. You are calling your method findBySampleClass, you are passing SampleClass as an input parameter and expect SampleClass as an output.This means that you would be returning the object itself from semantic point of view. This is suspicious. But going further on, you are doing something different inside the actual query specfication; you are using the status property of the object you passed in. This deviates from the convention that the method name should say what the query will be. In this case the correct(and most natural) way to go is to pass the status as a parameter, rename the method to findBySampleClassStatus or findByStatus.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 19 '18 at 13:26

























                  answered Nov 19 '18 at 13:18









                  NiVeRNiVeR

                  6,83141930




                  6,83141930























                      1














                      You could use the approach as mentioned in the JPA docs:



                          @Query(value = "SELECT * FROM Users u WHERE u.status = ?1",
                      nativeQuery = true)
                      List<SampleClass> findBySampleClass(String status);


                      Please refer to the official docs for further options : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_native_queries



                      Not sure why you are trying to pass the whole object though.Would you care to explain?






                      share|improve this answer





















                      • 1





                        While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

                        – Mihai Chelaru
                        Nov 19 '18 at 14:05











                      • @MihaiChelaru - Have updated the answer as suggested. Will keep the suggestion in mind for future answers.

                        – user3392782
                        Nov 19 '18 at 14:51
















                      1














                      You could use the approach as mentioned in the JPA docs:



                          @Query(value = "SELECT * FROM Users u WHERE u.status = ?1",
                      nativeQuery = true)
                      List<SampleClass> findBySampleClass(String status);


                      Please refer to the official docs for further options : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_native_queries



                      Not sure why you are trying to pass the whole object though.Would you care to explain?






                      share|improve this answer





















                      • 1





                        While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

                        – Mihai Chelaru
                        Nov 19 '18 at 14:05











                      • @MihaiChelaru - Have updated the answer as suggested. Will keep the suggestion in mind for future answers.

                        – user3392782
                        Nov 19 '18 at 14:51














                      1












                      1








                      1







                      You could use the approach as mentioned in the JPA docs:



                          @Query(value = "SELECT * FROM Users u WHERE u.status = ?1",
                      nativeQuery = true)
                      List<SampleClass> findBySampleClass(String status);


                      Please refer to the official docs for further options : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_native_queries



                      Not sure why you are trying to pass the whole object though.Would you care to explain?






                      share|improve this answer















                      You could use the approach as mentioned in the JPA docs:



                          @Query(value = "SELECT * FROM Users u WHERE u.status = ?1",
                      nativeQuery = true)
                      List<SampleClass> findBySampleClass(String status);


                      Please refer to the official docs for further options : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#_native_queries



                      Not sure why you are trying to pass the whole object though.Would you care to explain?







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 19 '18 at 15:00

























                      answered Nov 19 '18 at 13:07









                      user3392782user3392782

                      1614




                      1614








                      • 1





                        While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

                        – Mihai Chelaru
                        Nov 19 '18 at 14:05











                      • @MihaiChelaru - Have updated the answer as suggested. Will keep the suggestion in mind for future answers.

                        – user3392782
                        Nov 19 '18 at 14:51














                      • 1





                        While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

                        – Mihai Chelaru
                        Nov 19 '18 at 14:05











                      • @MihaiChelaru - Have updated the answer as suggested. Will keep the suggestion in mind for future answers.

                        – user3392782
                        Nov 19 '18 at 14:51








                      1




                      1





                      While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

                      – Mihai Chelaru
                      Nov 19 '18 at 14:05





                      While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

                      – Mihai Chelaru
                      Nov 19 '18 at 14:05













                      @MihaiChelaru - Have updated the answer as suggested. Will keep the suggestion in mind for future answers.

                      – user3392782
                      Nov 19 '18 at 14:51





                      @MihaiChelaru - Have updated the answer as suggested. Will keep the suggestion in mind for future answers.

                      – user3392782
                      Nov 19 '18 at 14:51



                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents