Hello all,
for performance reasons I'm using native queries, some of which return columns that are embedded objects. Unfortunately I've been unable to cast these columns to actual instances.
For example, consider the following (Ids and accessors ommitted for brevity's sake) :
Code:
class Location {
Float longitude;
Float latitude;
}
class Measurement {
Date date;
Location location;
Float value;
}
In the 'measurement' table storing Measure instances, the location attribute is stored as a byte array. Now, as I often need to extract only the location and value attributes, I stared building a native named query as follows :
Code:
@NamedNativeQuery(name="locsandvals",query="select m.location as location, m.value as value from measurements as m where (<whatever restriction I choose to impose>)",resultSetMapping="locsandvalsrmapping")
And now I'm unable to correctly define the actual resultSetMapping because I cannot define how the embedded Location instance maps to the query columns. I've tried something like
Code:
@SqlResultSetMapping(name="locsandvalsmapping",entities={@EntityResult(entityClass=Location.class)},columns={@ColumnResult(name="value")})
But Hibernate complains about not finding an 'id' column.
Alternatively, I tried to map everything to @ColumnResult and then cast the contents of the 'location' column to a 'Location' instance, like this :
Code:
@SqlResultSetMapping(name="locsandvalsmapping",columns={@ColumnResult(name="location"),@ColumnResult(name="value")})
and in the code, assuming 'rec' is a record returned by the named query :
Code:
Location loc=(Location)rec[0];
But this raises un exception because it is not possible to cast a byte array to a Location instance.
So I'd really appreciate if anyone having achieved the correct "extraction" of embedded instances through named queries shared their recipe with me.
MMM