Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 354572

Summary: TYPE() fails in projection query result: EclipseLink ConversionException
Product: z_Archived Reporter: darren
Component: EclipselinkAssignee: Project Inbox <eclipselink.orm-inbox>
Status: NEW --- QA Contact:
Severity: minor    
Priority: P2 CC: tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description darren CLA 2011-08-11 21:44:13 EDT
Build Identifier: Eclipse Persistence Services - 2.2.0.v20110202-r8913

When TYPE is used on an identification variable in part of the result of a projection query with a constructor expression (only), I get the following exception (as applied to a searching on an example entity Employee extends Element, with a constructor expression ElementDTO(Class,Long,String): 

-Exception [EclipseLink-3007] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.ConversionException
Exception in thread "main" Local Exception Stack: 
Exception Description: The object [Employee], of class [class java.lang.String], could not be converted to [class java.lang.Class].  Ensure that the class [Employee] is on the CLASSPATH.  You may need to use alternate API passing in the appropriate class loader as required, or setting it on the default ConversionManager
Internal Exception: java.lang.ClassNotFoundException: Employee

This happens whether or not I use application-managed or container-managed mode.

The problem is described with precise examples at: http://www.eclipse.org/forums/index.php/t/233012/

Reproducible: Always

Steps to Reproduce:
1. Create an entity class (such as Employee) that extends another entity class (such as Element), and ensure default single table ("super table) inheritance strategy is used, so DTYPE column will be generated. For the sake of this explanation, Element has fields 'id' and 'name' and Employee has additional field 'salary'.

2. Create a service that uses a projection query into a constructor expression such EmployeeDTO(Class entityClass, Long id, String name), where EmployeeDTO represents a transfer object to carry a minimal slice of a typically more complex Employee: 

    public List<EmployeeDTO> findAllEmployeeDTOs() {
        return em.createQuery(
        "SELECT NEW examples.model.EmployeeDTO(TYPE(e),e.id,e.name) "+
                "FROM Employee e "+ "ORDER BY e.name", EmployeeDTO.class).getResultList();        
    }

3.  Populate the database with some Employee objects, then run the projection query to trigger the error given above.

Please note that the projection query into Object[] (without a constructor expression) runs fine, and yields TYPE(e) as a 'class examples.model.Employee'. It is only when the projection query is fed into a constructor expression that the problem arises.

I've marked this as Major, since for my purposes this is a real show-stopper. It completely prevents my from migrating from full entity queries to minimalist projection queries in my application, since I definitely need to be able to expose in the user interface details of the entity class, and there is no other way of obtaining the information. So it prevents me from applying the project query strategy (a major feature) at all for my application.
Comment 1 darren CLA 2011-08-11 22:03:50 EDT
I've since realized that a workaround exists for me, namely since a projection query with TYPE() but without a constructor expression works, I can just perform the projection query without a constructor expression then iterate over the List<Object[]> result and feed the parts of the result into my desired constructor, as described here: http://www.eclipse.org/forums/index.php/mv/msg/233012/714991/#msg_714991

So I've downgraded importance to minor.
Comment 2 Tom Ware CLA 2011-08-24 10:10:45 EDT
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:07:30 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink