OpenJPA
  1. OpenJPA
  2. OPENJPA-2065

JPA 2.0 Spec. Violation? EntityManager#createNamedQuery(String, Class) bombs with non-entity Class parameter

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.1.0, 2.1.1, 2.2.0
    • Fix Version/s: 2.2.0
    • Component/s: jpa
    • Labels:

      Description

      Suppose we have an interface, A.

      Suppose we have a JPA-compliant entity class, B, that implements A.

      This construction will fail at runtime:

      final TypedQuery<A> q = em.createNamedQuery("someNamedQuery", A.class);

      Hibernate and EclipseLink both accept this construction.

      I don't see anywhere in the JPA specification that the class parameter must denote an entity type.

        Issue Links

          Activity

          Laird Nelson created issue -
          Laird Nelson made changes -
          Field Original Value New Value
          Description Suppose we have an interface, A.

          Suppose we have an entity, B, that implements A.

          This construction will fail at runtime:

          {code}
          final TypedQuery<A> q = em.createNamedQuery("someNamedQuery", A.class);
          {code}

          I don't see anywhere in the JPA specification that the class parameter must denote an entity type.
          Suppose we have an interface, A.

          Suppose we have an entity, B, that implements A.

          This construction will fail at runtime:

          final TypedQuery<A> q = em.createNamedQuery("someNamedQuery", A.class);

          I don't see anywhere in the JPA specification that the class parameter must denote an entity type.
          Laird Nelson made changes -
          Affects Version/s 2.1.1 [ 12316191 ]
          Description Suppose we have an interface, A.

          Suppose we have an entity, B, that implements A.

          This construction will fail at runtime:

          final TypedQuery<A> q = em.createNamedQuery("someNamedQuery", A.class);

          I don't see anywhere in the JPA specification that the class parameter must denote an entity type.
          Suppose we have an interface, A.

          Suppose we have a JPA-compliant entity class, B, that implements A.

          This construction will fail at runtime:

          final TypedQuery<A> q = em.createNamedQuery("someNamedQuery", A.class);

          Hibernate and EclipseLink both accept this construction.

          I don't see anywhere in the JPA specification that the class parameter must denote an entity type.
          Labels specification
          Hide
          Kevin Sutter added a comment -

          Laird,
          This does look to be a valid request, but I don't think it's a spec compliance issue. According to section 2.11.3, non-entity classes (interfaces) can not be passed as arguments to EM and Query methods:

          "Non-entity classes cannot be passed as arguments to methods of the EntityManager or Query
          interfaces[24] and cannot bear mapping information."

          An easy work around for now is to not utilize the TypedQuery support and cast the results instead. Not great, but it's workable until a better solution is developed.

          BTW, having both EclipseLink and Hibernate support this does add to the weight of resolving this JIRA...

          Thanks,
          Kevin

          Show
          Kevin Sutter added a comment - Laird, This does look to be a valid request, but I don't think it's a spec compliance issue. According to section 2.11.3, non-entity classes (interfaces) can not be passed as arguments to EM and Query methods: "Non-entity classes cannot be passed as arguments to methods of the EntityManager or Query interfaces [24] and cannot bear mapping information." An easy work around for now is to not utilize the TypedQuery support and cast the results instead. Not great, but it's workable until a better solution is developed. BTW, having both EclipseLink and Hibernate support this does add to the weight of resolving this JIRA... Thanks, Kevin
          Hide
          Laird Nelson added a comment -

          That would imply among other things that this too is illegal:

          final TypedQuery<Long> q = em.createNamedQuery("someQueryThatReturnsAnAggregateOrCOUNTResult", Long.class);

          And this:

          final TypedQuery<Object[]> q = em.createNamedQuery("someQueryThatReturnsAnObjectArrayBecauseOfStuffInTheSelectClause", Object[].class);

          Is that really the case? Section 2.11 has very narrow scope. 2.11.3 would seem to have been conceived of within the bounds of that scope, not within the bounds of the entire specification. Nevertheless, there it is. That is really unfortunate. I'll ask the sleepy users@jpa-spec.java.net list, where I appear to be the only poster, what they think.

          Show
          Laird Nelson added a comment - That would imply among other things that this too is illegal: final TypedQuery<Long> q = em.createNamedQuery("someQueryThatReturnsAnAggregateOrCOUNTResult", Long.class); And this: final TypedQuery<Object[]> q = em.createNamedQuery("someQueryThatReturnsAnObjectArrayBecauseOfStuffInTheSelectClause", Object[].class); Is that really the case? Section 2.11 has very narrow scope. 2.11.3 would seem to have been conceived of within the bounds of that scope, not within the bounds of the entire specification. Nevertheless, there it is. That is really unfortunate. I'll ask the sleepy users@jpa-spec.java.net list, where I appear to be the only poster, what they think.
          Hide
          Kevin Sutter added a comment -

          Fair enough, Laird. I didn't say it wasn't a valid request. I was just looking for a "get out of jail free" card for the spec violation statement... Thanks again for bringing this to our attention.

          Show
          Kevin Sutter added a comment - Fair enough, Laird. I didn't say it wasn't a valid request. I was just looking for a "get out of jail free" card for the spec violation statement... Thanks again for bringing this to our attention.
          Hide
          Laird Nelson added a comment -

          Sure; not trying to be difficult. Thanks for the quick response, and the pointers to 2.11.3. I'm guessing that this section (2.11.3) was created before there was a TypedQuery-returning method on EntityManager, and that the spec. authors would probably modify it if it were brought to their attention.

          Show
          Laird Nelson added a comment - Sure; not trying to be difficult. Thanks for the quick response, and the pointers to 2.11.3. I'm guessing that this section (2.11.3) was created before there was a TypedQuery-returning method on EntityManager, and that the spec. authors would probably modify it if it were brought to their attention.
          Hide
          Rick Curtis added a comment -

          Updating fix/affects version.

          Show
          Rick Curtis added a comment - Updating fix/affects version.
          Rick Curtis made changes -
          Fix Version/s 2.2.0 [ 12315910 ]
          Affects Version/s 2.2.0 [ 12315910 ]
          Rick Curtis made changes -
          Assignee Rick Curtis [ curtisr7 ]
          Hide
          Rick Curtis added a comment -

          Pinaki committed a code fix, and I committed a unit test. Marking issue as resolved.

          Show
          Rick Curtis added a comment - Pinaki committed a code fix, and I committed a unit test. Marking issue as resolved.
          Rick Curtis made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Resolution Fixed [ 1 ]
          Albert Lee made changes -
          Link This issue relates to OPENJPA-2071 [ OPENJPA-2071 ]
          Hide
          Albert Lee added a comment -

          Close issue in preparation for 2.2.0 release.

          Show
          Albert Lee added a comment - Close issue in preparation for 2.2.0 release.
          Albert Lee made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open Resolved Resolved
          10d 22h 52m 1 Rick Curtis 05/Nov/11 16:53
          Resolved Resolved Closed Closed
          89d 21m 1 Albert Lee 02/Feb/12 17:15

            People

            • Assignee:
              Rick Curtis
              Reporter:
              Laird Nelson
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development