将结果集映射到不是实体的自定义pojo

时间:2018-08-13 10:15:33

标签: java spring-data-jpa sqlresultsetmapping

我需要通过连接多个表来获取4列。我已经创建了查询。但是如何将结果集映射到不是实体的pojo,我想使用Spring Data JPA。

有人可以帮忙吗?

谢谢!

编辑 自定义POJO类:

`

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

    }

`

1 个答案:

答案 0 :(得分:0)

注释必须在实体上!将它们放在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")
                            }
                            )
                }
        )

另一种方法是使用QLRM摆脱这些注释。

看看GitHub页面:https://github.com/simasch/qlrm