Re: Java 1.5 enums - ClassCastException

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Wed Feb 01 2006 - 00:34:47 EST

  • Next message: Tore Halset: "Re: memory usage"

    In fact this wasn't a bug, just a misunderstanding. See my comment
    attached to the Jira issue:

        http://objectstyle.org/jira/secure/ViewIssue.jspa?key=CAY-433

    ...If the inner Enum class name is entered in the Modeler as
    mypackage.MyClass$MyEnum (notice the "$" sign), Cayenne would happily
    take it. Still I relaxed the rules adding support for inner classes
    mapped with "." instead of "$"

    So, you can use inner enums right away without waiting for the next
    milestone where the dots are supported.

    Andrus

    On Jan 30, 2006, at 1:46 AM, Ryan Holmes wrote:

    > That was the problem. Thanks for the answer and for opening a bug
    > report.
    >
    > Support for inner enum classes would be nice to have since I think
    > it's a fairly common idiom (a project at work has at least 100 of
    > them and moving them to top-level classes would not go over well).
    > Anyway, Cayenne's automatic enum support is fantastic and moving
    > enums into regular classes is no problem for the project I'm
    > working on.
    >
    > -Ryan
    >
    >
    > Andrus Adamchik wrote:
    >> Ryan,
    >>
    >> What I discovered when testing your case is that Cayenne can't
    >> handle an Enum which is an inner class. It has to be in its own
    >> top-level class. Once I moved it to the top, everything started to
    >> work.
    >>
    >> While I was getting a different error (ClassNotFoundException), I
    >> think the reason may be the same. I'll open a bug report regarding
    >> Enums as inner classes and see what we can do. In the meantime,
    >> could you please try the fix I described above.
    >>
    >> Andrus
    >>
    >>
    >> On Jan 29, 2006, at 7:27 PM, Ryan Holmes wrote:
    >>
    >>> I'm trying to use JDK 1.5 enums in Cayenne 1.2M10 (Windows
    >>> modeler) with no luck so far. A ClassCastException is thrown when
    >>> invoking the superclass getter for the enum property.
    >>>
    >>> My enum type (com.hyperstep.bulldog.domain.User.UserStatus) is
    >>> specified as the "Java Type" in the ObjEntity attribute. The
    >>> corresponding db attribute is a VARCHAR. The relevant parts of
    >>> the generated superclass look like this:
    >>>
    >>> public class _User extends
    >>> org.objectstyle.cayenne.CayenneDataObject {
    >>> ...
    >>> public static final String STATUS_PROPERTY = "status";
    >>> ...
    >>> public void setStatus
    >>> (com.hyperstep.bulldog.domain.User.UserStatus status) {
    >>> writeProperty("status", status);
    >>> }
    >>> public com.hyperstep.bulldog.domain.User.UserStatus getStatus() {
    >>> return (com.hyperstep.bulldog.domain.User.UserStatus)
    >>> readProperty("status");
    >>> }
    >>> ...
    >>> }
    >>>
    >>> UserStatus is a simple enum inside my User domain class:
    >>>
    >>> public class User extends _User {
    >>> ...
    >>> public static enum UserStatus {
    >>> REGISTERED, AUTHORIZED, LOCKED_OUT, BANNED, DELETED
    >>> }
    >>> ...
    >>> }
    >>>
    >>> setStatus() works fine and the correct UserStatus name is saved
    >>> to the db. The ClassCastException occurs after fetching the
    >>> object and calling getStatus():
    >>>
    >>> java.lang.ClassCastException: java.lang.String
    >>> at com.hyperstep.bulldog.domain.auto._User.getStatus
    >>> (_User.java:150)
    >>>
    >>> I'm probably missing something really simple here, but I'm new to
    >>> Cayenne and not sure what to try next.
    >>> FYI, I noticed that the modeler is using version 1.1 templates
    >>> and I don't know how to specify 1.2 templates or if that's even
    >>> related to the enum problem.
    >>
    >>
    >
    >



    This archive was generated by hypermail 2.0.0 : Wed Feb 01 2006 - 00:34:50 EST