Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 332094 - Enum constant processing breaks when toString() has been overriden
Summary: Enum constant processing breaks when toString() has been overriden
Status: CLOSED DUPLICATE of bug 332207
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P2 normal (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-07 17:59 EST by Nikita Zinoviev CLA
Modified: 2022-06-09 10:19 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nikita Zinoviev CLA 2010-12-07 17:59:44 EST
Build Identifier: 2.1.0-M7

I have an enum class with overriden toString() which prints out the value in a user friendly form (say "Closed" for CLOSED enum constant). When I try to use it inside a query ... WHERE entity.status=mypackage.Myenum.CLOSED I get an error

Caused by: Exception [EclipseLink-8015] (Eclipse Persistence Services - 2.1.0.v20100127-r6421): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [SELECT .. FROM myEntity entity
WHERE entity.status=mypackage.Myenum.CLOSED], line
1, column 88: invalid enum literal, the enum type
mypackage.Myenum.CLOSED does not have an enum literal
CLOSED.

        at org.eclipse.persistence.exceptions.JPQLException.invalidEnumLiteral(JPQLException.java:260)
        at org.eclipse.persistence.internal.jpa.parsing.DotNode.validate(DotNode.java:91)
        at org.eclipse.persistence.internal.jpa.parsing.Node.validate(Node.java:94)
        at org.eclipse.persistence.internal.jpa.parsing.BinaryOperatorNode.validate(BinaryOperatorNode.java:34)
        at org.eclipse.persistence.internal.jpa.parsing.EqualsNode.validate(EqualsNode.java:41)
        at org.eclipse.persistence.internal.jpa.parsing.WhereNode.validate(WhereNode.java:34)
        at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:211)
        at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:187)
        at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:177)
        at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateReadQueryInternal(JPQLParseTree.java:110)
        at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateQuery(JPQLParseTree.java:84)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:207)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:178)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:130)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:114)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1338)
 

When I looked at 
/Users/nikitazinoviev/.m2/repository/org/eclipse/persistence/eclipselink/2.1.0-M7/eclipselink-2.1.0-M7-sources.jar!/org/eclipse/persistence/internal/jpa/parsing/TypeHelperImpl.java

I found

    /** Returns the enum constant if the specified type denotes an enum type
     * and the specified constant denotes a constant of the enum type. 
     */
    public Object resolveEnumConstant(Object type, String constant) {
        Class clazz = getJavaClass(type);
        Object[] constants = clazz.getEnumConstants();
        if (constants != null) {
            for (int i = 0; i < constants.length; i++) {
                if (constant.equals(constants[i].toString())) {
                    return constants[i];
                }
            }
        }
        return null;
    }

probably this method should cast to Enum and use name() instead of toString().

Reproducible: Always

Steps to Reproduce:
1. Create an enum overriding toString().
2. Add a field of this type to some JPA entity via
    @Enumerated(EnumType.STRING)
    private MyEnum status;

3. Execute a query with ... WHERE entity.status=mypackage.Myenum.CLOSED
Comment 1 Tom Ware CLA 2010-12-17 09:07:13 EST
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 2 Tom Ware CLA 2010-12-17 09:18:49 EST

*** This bug has been marked as a duplicate of bug 332207 ***
Comment 3 Eclipse Webmaster CLA 2022-06-09 10:16:06 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:19:34 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink