hibernate - jpa 2.1 中的@ConstructorResult 映射

标签 hibernate jpa

我正在尝试将 JPA native 查询的返回结果映射到未映射的 Java POJO 类 并使用 @ConstructorResult 如下:

@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 类是:

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 数组。

我想要一个像这样的 ProductDetailBO:

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

我该怎么做?

最佳答案

我刚刚在我的项目中对一个未管理的 pojo 执行了类似的映射。问题是您应该在创建查询时指向 SqlResultSetMapping 的名称,因此在您的情况下它应该是这样的:

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

然后是这样的:

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

关于hibernate - jpa 2.1 中的@ConstructorResult 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423604/

相关文章:

java - 持久化实体时的问题

java - hibernate 默认值

java - 从 hibernate session 中删除对象?

java - Hibernate manyToMany 只返回一个元素

java - 在 Spring 应用程序中使用 Hibernate 自动创建表

java - 将自定义方法添加到 Spring Data JPA

java - 在没有本地 persistence.xml 的情况下使用 JPA @StaticMetamodel 类? (远程 EJB)

hibernate - 在 PostgreSQL 和 SpringBoot (JPA) 中创建序列的问题

Java Hibernate 奇怪的编码错误?

mysql - 使用同一实体将此关系映射到 2 个实体