Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » RESOLVED: retrieving some fields from an Entity(I need to load Entities but not with all its attributes (there are too many))
RESOLVED: retrieving some fields from an Entity [message #826037] Wed, 21 March 2012 11:20 Go to next message
FiruzzZ Mising name is currently offline FiruzzZ Mising name
Messages: 19
Registered: October 2011
Junior Member
The class Producto got more than 20 attributes, but i need to performance a faster and always QueryHints.REFRESH == TRUE query AND just get 3 fields (id, code, name)

I was using this one (when i used TopLink)...
@SqlResultSetMappings({
    //para los ComboBox
    @SqlResultSetMapping(name = "ProductoToBuscador", entities = {
        @EntityResult(entityClass = Producto.class, fields = {
            @FieldResult(name = "id", column = "id"),
            @FieldResult(name = "codigo", column = "codigo"),
            @FieldResult(name = "nombre", column = "nombre")
        })
    }),


..but doesn't work with Eclipselink, it's throwing:
java.lang.ClassCastException: org.eclipse.persistence.queries.ResultSetMappingQuery cannot be cast to org.eclipse.persistence.queries.ObjectLevelReadQuery


Some ppl say that the NativeQuery should retrieve all the fields of the entity ( "SELECT p.*"), another say just what you need ( SELECT p.id, p.codigo, p.nombre), but noone of these are working..

I really need a hand on this.. thanks in advances for any help

[Updated on: Thu, 22 March 2012 10:13]

Report message to a moderator

RESOLVED: retrieving some fields from an Entity [message #826794 is a reply to message #826037] Thu, 22 March 2012 10:12 Go to previous messageGo to next message
FiruzzZ Mising name is currently offline FiruzzZ Mising name
Messages: 19
Registered: October 2011
Junior Member
well.. with just executing a native query (SELECT p.id, p.codigo, p.nombre....) WITHOUT a SQLResultSetMap resulted be enough to recovered the instance(s) just like I wanted..
public Query createNativeQuery(String sqlString);
Re: RESOLVED: retrieving some fields from an Entity [message #826841 is a reply to message #826794] Thu, 22 March 2012 11:15 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1016
Registered: July 2009
Senior Member
The only reason to use a SqlResultSetMapping instead of a regular native query when returning a single entity is if the field names returned from the native query do not match the names defined in the mapings. The SqlResultSetMapping allow matching them up as required, or returning other data options that are not available in the general native query.

Just a warning though - by retreiving only 3 fields for the entity which is managed you are potentially corrupting your cache, since only those 3 fields will be visible. That means later on a em.find() will return Producto entities with only those 3 fields populated for as long as that context is used, and potentially put them into the shared cache as well.

Any reason for using the native query instead of using JPQL? A fetch/attribute group could be used so that it only fetches the 3 attributes needed, with the rest lazy loaded if accessed later on.
http://wiki.eclipse.org/EclipseLink/Examples/JPA/AttributeGroup
This would allow the refresh and prevent corupting your cache.

As for the ClassCastException - this looks like a bug that might have been fixed in a later EclipseLink version. Can you try the latest and file a bug with the stack trace and the query hints being used if you still get it?

Best Regards,
Chris
Previous Topic:ConcurrencyException
Next Topic:Some Discriminator Values cause problem, while others do not
Goto Forum:
  


Current Time: Thu Jul 24 23:45:00 EDT 2014

Powered by FUDForum. Page generated in 0.10957 seconds