当前位置: 首页 > 知识库问答 >
问题:

jpa 2.1中的@ConstructorResult映射

慕阳文
2023-03-14

我试图将JPA本机查询的返回结果映射到未映射的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) }) })
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;

    }


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

我怎么能那么做?

共有1个答案

上官正志
2023-03-14

在我的项目中,我只是对一个未加映射的pojo执行了类似的映射。问题是,在创建查询时,您应该指向SqlResultSetMapping的名称,因此在您的示例中应该如下所示:

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

然后类似于:

final List<ProductDetailBO> result = Collections.checkedList(query.getResultList(), ProductDetailBO.class);
 类似资料:
  • 我使用JPA2.1和Hibernate来实现。 举个示例: 我有一个父实体(称为父实体)和一个子实体(称为子实体)。 可嵌入唯一密钥: 父实体: 子实体: 对于一些关于Parent查询,我需要child,而对于某些情况,我不需要。 我试图通过uniqueKey获取数据,但没有调用getChild(),但它仍然在获取父级数据的同时获取子级数据。 在参考了这个答案之后。它说在@OneToOne的情况下

  • 我正在尝试将我的项目配置为在Weblogic 12.1.3上使用Hibernate Validator 5.1.2运行JPA2.1,但当我尝试部署我的项目时遇到了问题。 我使用手动方法将weblogic配置为使用JPA2.1,如本weblogic 12.1.3文档页面所述:http://docs.oracle.com/middleware/1213/wls/EJBAD/using_toplink.

  • 在JavaEE环境中,我将JPA2.1实现与EclipseLink一起使用, CarColor转换器: persistence.xml > 当我在persistence.xml文件上注释转换器的声明,并试图将我的实体持久化到数据库中时,会出现这样的错误:“请确保转换器类名正确,并且存在于持久化单元定义中。”并且没有编译时间异常,只有一个非常明确的警告:

  • 我有一个2.1.4版本的项目,带有spring boot数据。 该项目具有以下关系实体:ApplicationEntity ApplicationTranslateEntity LanguageEntity它在数据库中有一个区域设置关系表(ManyToMany),该表中有一个用于不同语言的文本的额外表(ApplicationTranslateEntity)。 org.hibernate.Query

  • 但我得到以下错误: 已迁移到新包的该类: 然而,我没有找到在MAVEN Build中继续生成DDL创建脚本的明确方法。 那又怎样?它不是应该支持的一个主要特性吗?怎么做?

  • 将变量绑定到本机查询时出错。EclipseLink(JPA2.1) 内部异常:org.PostgreSQL.util.psqlException:错误:“$1”位置或附近的语法错误:12错误代码:0线程“awt-eventqueue-0”javax.Persistence.persistenceException:Exception[EclipseLink-4002](Eclipse Persis