spring boot data @query to DTO












0















I want to assign the result of a query to DTO object. The DTO looks this



@Getter
@Setter
@NoArgsConstructor
public class Metric {
private int share;
private int shareholder;

public Metric(int share, int shareholder) {
this.share = share;
this.shareholder = shareholder;
}

}


and the looks as following



@RepositoryRestResource(collectionResourceRel = "shareholders", path = 
"shareholders")
public interface ShareholderRepository extends
PagingAndSortingRepository<Shareholder, Integer> {

@Query(value = "SELECT new
com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM
shareholders s WHERE s.attend=true")
Metric getMetrics();


}


However, this is not working as I get the following exception:



Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]









share|improve this question




















  • 1





    whats the exception?

    – Maciej Kowalski
    Nov 15 '18 at 15:07











  • @MaciejKowalski This's is the exception raised. Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]

    – btinsae
    Nov 15 '18 at 16:16













  • It seems your query is a native query (!= JPQL or HQL). In that case, specify it in the annotation like : @Query(value = "sql string ", nativeQuery = true)

    – Guillaume Husta
    Nov 16 '18 at 8:54
















0















I want to assign the result of a query to DTO object. The DTO looks this



@Getter
@Setter
@NoArgsConstructor
public class Metric {
private int share;
private int shareholder;

public Metric(int share, int shareholder) {
this.share = share;
this.shareholder = shareholder;
}

}


and the looks as following



@RepositoryRestResource(collectionResourceRel = "shareholders", path = 
"shareholders")
public interface ShareholderRepository extends
PagingAndSortingRepository<Shareholder, Integer> {

@Query(value = "SELECT new
com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM
shareholders s WHERE s.attend=true")
Metric getMetrics();


}


However, this is not working as I get the following exception:



Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]









share|improve this question




















  • 1





    whats the exception?

    – Maciej Kowalski
    Nov 15 '18 at 15:07











  • @MaciejKowalski This's is the exception raised. Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]

    – btinsae
    Nov 15 '18 at 16:16













  • It seems your query is a native query (!= JPQL or HQL). In that case, specify it in the annotation like : @Query(value = "sql string ", nativeQuery = true)

    – Guillaume Husta
    Nov 16 '18 at 8:54














0












0








0


1






I want to assign the result of a query to DTO object. The DTO looks this



@Getter
@Setter
@NoArgsConstructor
public class Metric {
private int share;
private int shareholder;

public Metric(int share, int shareholder) {
this.share = share;
this.shareholder = shareholder;
}

}


and the looks as following



@RepositoryRestResource(collectionResourceRel = "shareholders", path = 
"shareholders")
public interface ShareholderRepository extends
PagingAndSortingRepository<Shareholder, Integer> {

@Query(value = "SELECT new
com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM
shareholders s WHERE s.attend=true")
Metric getMetrics();


}


However, this is not working as I get the following exception:



Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]









share|improve this question
















I want to assign the result of a query to DTO object. The DTO looks this



@Getter
@Setter
@NoArgsConstructor
public class Metric {
private int share;
private int shareholder;

public Metric(int share, int shareholder) {
this.share = share;
this.shareholder = shareholder;
}

}


and the looks as following



@RepositoryRestResource(collectionResourceRel = "shareholders", path = 
"shareholders")
public interface ShareholderRepository extends
PagingAndSortingRepository<Shareholder, Integer> {

@Query(value = "SELECT new
com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM
shareholders s WHERE s.attend=true")
Metric getMetrics();


}


However, this is not working as I get the following exception:



Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]






spring-boot spring-data-jpa dto






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 12:39









g00glen00b

23.2k85279




23.2k85279










asked Nov 15 '18 at 14:58









btinsaebtinsae

215




215








  • 1





    whats the exception?

    – Maciej Kowalski
    Nov 15 '18 at 15:07











  • @MaciejKowalski This's is the exception raised. Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]

    – btinsae
    Nov 15 '18 at 16:16













  • It seems your query is a native query (!= JPQL or HQL). In that case, specify it in the annotation like : @Query(value = "sql string ", nativeQuery = true)

    – Guillaume Husta
    Nov 16 '18 at 8:54














  • 1





    whats the exception?

    – Maciej Kowalski
    Nov 15 '18 at 15:07











  • @MaciejKowalski This's is the exception raised. Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]

    – btinsae
    Nov 15 '18 at 16:16













  • It seems your query is a native query (!= JPQL or HQL). In that case, specify it in the annotation like : @Query(value = "sql string ", nativeQuery = true)

    – Guillaume Husta
    Nov 16 '18 at 8:54








1




1





whats the exception?

– Maciej Kowalski
Nov 15 '18 at 15:07





whats the exception?

– Maciej Kowalski
Nov 15 '18 at 15:07













@MaciejKowalski This's is the exception raised. Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]

– btinsae
Nov 15 '18 at 16:16







@MaciejKowalski This's is the exception raised. Caused by:org.hibernate.QueryException: could not resolve property: no_of_shares of:com.company.shareholders.sh.Shareholder[SELECT new com.company.shareholders.sh.Metric(SUM(s.no_of_shares),COUNT(*)) FROM com.company.shareholders.sh.Shareholder s WHERE s.attend=true]

– btinsae
Nov 15 '18 at 16:16















It seems your query is a native query (!= JPQL or HQL). In that case, specify it in the annotation like : @Query(value = "sql string ", nativeQuery = true)

– Guillaume Husta
Nov 16 '18 at 8:54





It seems your query is a native query (!= JPQL or HQL). In that case, specify it in the annotation like : @Query(value = "sql string ", nativeQuery = true)

– Guillaume Husta
Nov 16 '18 at 8:54












1 Answer
1






active

oldest

votes


















0














First, you can have a look at the Spring Data JPA documentation, you can find some help at this section : Class-based Projections (DTOs).



There is also a paragraph titled Avoid boilerplate code for projection DTOs, where they advise you to use Lombok's @Value annotation, to produce an immutable DTO. This is similar to Lombok's @Data annotation, but immutable.



If you apply it to your example, the source will look like :



@Value
public class MetricDto {

private int share;
private int shareholder;

}


Then, as your query is a NativeQuery, specifiy it in your Spring Data Repository.
You can find help in the documentation : Native Queries.
You will need something like :



@Query(value = "SELECT new 
com.company.shareholders.sh.MetricDto(SUM(s.no_of_shares),COUNT(*)) FROM
shareholders s WHERE s.attend=true", nativeQuery = true)
MetricDto getMetrics();





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%2f53322203%2fspring-boot-data-query-to-dto%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









    0














    First, you can have a look at the Spring Data JPA documentation, you can find some help at this section : Class-based Projections (DTOs).



    There is also a paragraph titled Avoid boilerplate code for projection DTOs, where they advise you to use Lombok's @Value annotation, to produce an immutable DTO. This is similar to Lombok's @Data annotation, but immutable.



    If you apply it to your example, the source will look like :



    @Value
    public class MetricDto {

    private int share;
    private int shareholder;

    }


    Then, as your query is a NativeQuery, specifiy it in your Spring Data Repository.
    You can find help in the documentation : Native Queries.
    You will need something like :



    @Query(value = "SELECT new 
    com.company.shareholders.sh.MetricDto(SUM(s.no_of_shares),COUNT(*)) FROM
    shareholders s WHERE s.attend=true", nativeQuery = true)
    MetricDto getMetrics();





    share|improve this answer




























      0














      First, you can have a look at the Spring Data JPA documentation, you can find some help at this section : Class-based Projections (DTOs).



      There is also a paragraph titled Avoid boilerplate code for projection DTOs, where they advise you to use Lombok's @Value annotation, to produce an immutable DTO. This is similar to Lombok's @Data annotation, but immutable.



      If you apply it to your example, the source will look like :



      @Value
      public class MetricDto {

      private int share;
      private int shareholder;

      }


      Then, as your query is a NativeQuery, specifiy it in your Spring Data Repository.
      You can find help in the documentation : Native Queries.
      You will need something like :



      @Query(value = "SELECT new 
      com.company.shareholders.sh.MetricDto(SUM(s.no_of_shares),COUNT(*)) FROM
      shareholders s WHERE s.attend=true", nativeQuery = true)
      MetricDto getMetrics();





      share|improve this answer


























        0












        0








        0







        First, you can have a look at the Spring Data JPA documentation, you can find some help at this section : Class-based Projections (DTOs).



        There is also a paragraph titled Avoid boilerplate code for projection DTOs, where they advise you to use Lombok's @Value annotation, to produce an immutable DTO. This is similar to Lombok's @Data annotation, but immutable.



        If you apply it to your example, the source will look like :



        @Value
        public class MetricDto {

        private int share;
        private int shareholder;

        }


        Then, as your query is a NativeQuery, specifiy it in your Spring Data Repository.
        You can find help in the documentation : Native Queries.
        You will need something like :



        @Query(value = "SELECT new 
        com.company.shareholders.sh.MetricDto(SUM(s.no_of_shares),COUNT(*)) FROM
        shareholders s WHERE s.attend=true", nativeQuery = true)
        MetricDto getMetrics();





        share|improve this answer













        First, you can have a look at the Spring Data JPA documentation, you can find some help at this section : Class-based Projections (DTOs).



        There is also a paragraph titled Avoid boilerplate code for projection DTOs, where they advise you to use Lombok's @Value annotation, to produce an immutable DTO. This is similar to Lombok's @Data annotation, but immutable.



        If you apply it to your example, the source will look like :



        @Value
        public class MetricDto {

        private int share;
        private int shareholder;

        }


        Then, as your query is a NativeQuery, specifiy it in your Spring Data Repository.
        You can find help in the documentation : Native Queries.
        You will need something like :



        @Query(value = "SELECT new 
        com.company.shareholders.sh.MetricDto(SUM(s.no_of_shares),COUNT(*)) FROM
        shareholders s WHERE s.attend=true", nativeQuery = true)
        MetricDto getMetrics();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 9:24









        Guillaume HustaGuillaume Husta

        1,9951630




        1,9951630
































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322203%2fspring-boot-data-query-to-dto%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

            Bressuire

            Vorschmack

            Quarantine