0

我需要通过加入多个表来获取 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;

    }

`

4

1 回答 1

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

于 2018-08-13T15:33:06.140 回答