0

I need to fetch 4 columns by joining multiple tables. I have created the query. But how can I map the result set to a pojo which is not an entity and i want to use Spring Data JPA.

Could someone please help?

Thank you!

EDIT Custom POJO class:

`

@Data
    @AllArgsConstructor
    @NamedNativeQuery(name = CustomPojo.retriveCustomPojo
            query = Constants.CUSTOM_QUERY, resultSetMapping = CustomDataMapping")
    @SqlResultSetMapping(name = "CustomDataMapping",
            classes = {
                    @ConstructorResult(
                            targetClass = CustomPojo.class,
                            columns = {
                                    @ColumnResult(name = "NAME"),
                                    @ColumnResult(name = "TYPE"),
                                    @ColumnResult(name = "TITLE"),
                                    @ColumnResult(name = "DESCRIPTION")
                            }
                            )
                }
        )
    public class CustomPojo implements Serializable {

        private static final long serialVersionUID = 1L;

        private String name
        private String type;
        private String title;
        private String description;

    }

`

Pavan
  • 217
  • 1
  • 4
  • 12
  • show your entity class and query – pvpkiran Aug 13 '18 at 10:18
  • 1
    https://stackoverflow.com/questions/21956042/mapping-a-jdbc-resultset-to-an-object this might help – miiiii Aug 13 '18 at 10:19
  • @Mad Man - I am using Spring Data JPA. I cannot use that. – Pavan Aug 13 '18 at 10:25
  • @pvpkiran - I have added the Pojo class. NOTE: Its not entity. From the query I want to directly map the result which will have 4 columns to this Pojo. – Pavan Aug 13 '18 at 10:26
  • https://stackoverflow.com/questions/29082749/spring-data-jpa-map-the-native-query-result-to-non-entity-pojo what abt this? – miiiii Aug 13 '18 at 10:27
  • Its not working. Whatever I am trying its not possible. Because for to use `@SqlResultSetMapping` class must be entity class. Can anyone confirm? If that's true, how can I achieve my scenario? – Pavan Aug 13 '18 at 10:30

1 Answers1

0

The annotations must be on an entity! It's not possible to have them on a POJO.

@NamedNativeQuery(name = CustomPojo.retriveCustomPojo
            query = Constants.CUSTOM_QUERY, resultSetMapping = CustomDataMapping")
@SqlResultSetMapping(name = "CustomDataMapping",
            classes = {
                    @ConstructorResult(
                            targetClass = CustomPojo.class,
                            columns = {
                                    @ColumnResult(name = "NAME"),
                                    @ColumnResult(name = "TYPE"),
                                    @ColumnResult(name = "TITLE"),
                                    @ColumnResult(name = "DESCRIPTION")
                            }
                            )
                }
        )

Another way is to use QLRM to get rid of these annotations.

Have a look at the GitHub Page: https://github.com/simasch/qlrm

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • I got it solved. I applied the annotations on the entity. But now i am getting this error `Got different size of tuples and aliases` – Pavan Aug 14 '18 at 05:15
  • How does Constants.CUSTOM_QUERY look like? – Simon Martinelli Aug 14 '18 at 08:12
  • Actually, this [link] https://stackoverflow.com/questions/49056084/got-different-size-of-tuples-and-aliases-exception-after-spring-boot-2-0-0-rel/49985099#49985099 worked. second answer. But not sure that is permanent solution or not. – Pavan Aug 14 '18 at 10:19
  • Here is my query `select book.name, book.type, title.title, title.description from book book join details title on (title.id = book.id)` – Pavan Aug 14 '18 at 10:23
  • And this is my interface method `@Query(name = "CustomPojo.retriveCustomPojo", nativeQuery = true) List retriveCustomPojo();` – Pavan Aug 14 '18 at 10:25
  • and the types of the columns name, type, title and description are all String? – Simon Martinelli Aug 14 '18 at 10:45