【问题标题】:Mapping an SQL native query to a POJO class将 SQL 原生查询映射到 POJO 类
【发布时间】:2018-09-11 13:43:54
【问题描述】:

我尝试使用@SqlResultSetMapping@ConstructorResult 将本机SQL 查询映射到POJO 类,如下所示:

@SqlResultSetMapping(
        name = "AnomalieMapping",
        classes = @ConstructorResult(
                targetClass = Anomalie.class,
                columns = {
                        @ColumnResult(name = "anomalieElement", type = String.class),
                        @ColumnResult(name = "anomalieType", type = String.class),
                        @ColumnResult(name = "libelle", type = String.class) }))
public class Anomalie {

    private ElementAnomalieEnum anomalieElement;
    private TypeAnomalieEnum anomalieType;
    private String libelle;

    public Anomalie() {
        super();
    }

    public Anomalie(final String libelle, final String anomalieElement, final String anomalieType) {
        super();
        this.libelle = libelle;
        this.anomalieElement = ElementAnomalieEnum.valueOf(StringUtils.stripAccents(anomalieElement.toUpperCase()));
        this.anomalieType = TypeAnomalieEnum.valueOf(StringUtils.stripAccents(anomalieType.substring(5).toUpperCase()));

    }
//Getters and Setters
}

然后为了在创建本地查询时使用声明的结果集映射,我通过它的名称来引用它:

Query query = entityManager.createNativeQuery(sqlQuery, "AnomalieMapping");
return query.getResultList();

但这对我不起作用,我收到以下错误:

org.hibernate.MappingException: 未知的 SqlResultSetMapping [异常映射]

这是我在 SGBD 中执行 SQL 查询时生成的内容:

【问题讨论】:

  • 这个注解是否在您的 JPA 提供者处理的类中?
  • @DN1 我该如何检查?
  • 只有你知道它是在什么类上指定的,或者那个类是否是一个实体,或者它是否在persistence.xml中指定
  • @hasnae 该问题的解决方案是添加在我的情况下不起作用的 Entity 注释。

标签: java hibernate jpa jpa-2.1 sqlresultsetmapping


【解决方案1】:

这就是我解决问题的方法:

我没有声明 @SqlResultSetMapping 注释,而是在 orm.xml 文件中声明如下:

<sql-result-set-mapping name="AnomalieMapping">
        <constructor-result target-class="xxx.Anomalie">
            <column name="libelle"/>
            <column name="anomalieElement"/>
            <column name="anomalieType"/>
        </constructor-result>
    </sql-result-set-mapping>

然后在我的 DAO 中,我得到如下结果:

Query query = entityManager.createNativeQuery(sqlQuery, "AnomalieMapping");
return query.getResultList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-06
    • 2015-05-18
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2020-06-11
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多