Jpa 2.1中的@ConstructorResult映射 [英] @ConstructorResult mapping in jpa 2.1

查看:111
本文介绍了Jpa 2.1中的@ConstructorResult映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将返回的JPA本机查询结果映射到未映射的Java POJO类
,并使用 @ConstructorResult 如下所示:

I am trying to map a returned result of JPA native query to a un-mapped Java POJO class and using @ConstructorResult as below:

@SqlResultSetMapping(name = "productDetailResult", classes = {
   @ConstructorResult(targetClass = com.rjil.mhood.jiostore.bo.ProductDetailBO.class,
   columns = {
@ColumnResult(name = "iconId", type = Integer.class),
@ColumnResult(name = "thumbnailId", type = Integer.class),
@ColumnResult(name = "screenshotId1", type = Integer.class),
@ColumnResult(name = "screenshotId2", type = Integer.class),
@ColumnResult(name = "screenshotId3", type = Integer.class),
@ColumnResult(name = "screenshotId4", type = Integer.class),
@ColumnResult(name = "screenshotId5", type = Integer.class),
@ColumnResult(name = "name", type = String.class),
@ColumnResult(name = "description", type = String.class),
@ColumnResult(name = "downloadcount", type = BigDecimal.class),
@ColumnResult(name = "artifactSize", type = String.class),
@ColumnResult(name = "creationDate", type = Date.class),
@ColumnResult(name = "updatedDate", type = Date.class),
@ColumnResult(name = "price", type = BigDecimal.class),
@ColumnResult(name = "downloadUrl", type = String.class),
@ColumnResult(name = "contentProviderName", type = String.class) }) })

和POJO class是:

and the POJO class is :

public class ProductDetailBO {

    private Integer iconId;
    private Integer thumbnailId;
    private Integer screenshotId1;
    private Integer screenshotId2;
    private Integer screenshotId3;
    private Integer screenshotId4;
    private Integer screenshotId5;
    private String name;
    private String description;
    private BigDecimal downloadCount;
    private String artifactSize;
    private Date creationDate;
    private Date updatedDate;
    private BigDecimal price;
    private String downloadUrl;
    private String contentProviderName;

    public ProductDetailBO() {

    }

    public ProductDetailBO(Integer iconId, Integer thumbnailId,
            Integer screenshotId1, Integer screenshotId2,
            Integer screenshotId3, Integer screenshotId4,
            Integer screenshotId5, String name, String description,
            BigDecimal downloadCount, String artifactSize, Date creationDate,
            Date updatedDate, BigDecimal price, String downloadUrl,
            String contentProviderName) {
        this.iconId = iconId;
        this.thumbnailId = thumbnailId;
        this.screenshotId1 = screenshotId1;
        this.screenshotId2 = screenshotId2;
        this.screenshotId3 = screenshotId3;
        this.screenshotId4 = screenshotId4;
        this.screenshotId5 = screenshotId5;
        this.name = name;
        this.price = price;
        this.creationDate = creationDate;
        this.updatedDate = updatedDate;
        this.description = description;
        this.downloadCount = downloadCount;
        this.artifactSize = artifactSize;
        this.downloadUrl = downloadUrl;
        this.contentProviderName = contentProviderName;

    }


}

POJO类包含一个匹配的构造函数,但 query.getResultList()返回一个 Object 数组。

The POJO class contains a matching constructor but the query.getResultList() returns an Object array.

我想要一个 ProductDetailBO 就像这样:

I want a ProductDetailBO like so:

ProductDetailBO bo = query.getResultList("query","ProductDetailBO").get(0);

我该怎么做?

How can I do that?

推荐答案

我只是在我的项目中执行了一个类似映射到一个unapped pojo。问题是你应该在创建查询时指向你的SqlResultSetMapping的名字,所以在你的情况下应该是这样的:

I just performed similar mapping to an un-manapped pojo in my project. The thing is that you should point to the name of your SqlResultSetMapping while creating a query, so in your case it should be something like this:

final Query query = entityManager.createNativeQuery(queryString, "productDetailResult");

然后类似:

and then something like:

final List<ProductDetailBO> result = Collections.checkedList(query.getResultList(), ProductDetailBO.class);

这篇关于Jpa 2.1中的@ConstructorResult映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆