-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Native query resultset mapping
PostPosted: Wed Jan 05, 2011 11:03 am 
Newbie

Joined: Mon Nov 08, 2010 11:01 am
Posts: 3
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.