简体   繁体   中英

Map result set to custom pojo which is not entity

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;

    }

`

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM