Reputation: 2084
I tried to map a native SQL query to a POJO class using @ConstructorResult
of @SqlResultSetMapping
as following :
@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
}
And then to use the declared result-set mapping in creating the native query, I reference it by its name:
Query query = entityManager.createNativeQuery(sqlQuery, "AnomalieMapping");
return query.getResultList();
But this didn't work for me I get the following error :
org.hibernate.MappingException: Unknown SqlResultSetMapping [AnomalieMapping]
This is what my SQL query is generating when I execute it in the SGBD:
Upvotes: 1
Views: 1847
Reputation: 2084
This is how I solved my problem:
Instead of declaring @SqlResultSetMapping
annotation I declared it in orm.xml
file as following:
<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>
And then in my DAO I get the result as following:
Query query = entityManager.createNativeQuery(sqlQuery, "AnomalieMapping");
return query.getResultList();
Upvotes: 1