Uploaded image for project: 'OpenJPA'
  1. OpenJPA
  2. OPENJPA-2065

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

    Details

    • Type: Bug
    • Status: Closed
    • Priority: 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

          Hide
          kwsutter 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
          kwsutter 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
          ljnelson 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
          ljnelson 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
          kwsutter 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
          kwsutter 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
          ljnelson 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
          ljnelson 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
          curtisr7 Rick Curtis added a comment -

          Updating fix/affects version.

          Show
          curtisr7 Rick Curtis added a comment - Updating fix/affects version.
          Hide
          curtisr7 Rick Curtis added a comment -

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

          Show
          curtisr7 Rick Curtis added a comment - Pinaki committed a code fix, and I committed a unit test. Marking issue as resolved.
          Hide
          allee8285 Albert Lee added a comment -

          Close issue in preparation for 2.2.0 release.

          Show
          allee8285 Albert Lee added a comment - Close issue in preparation for 2.2.0 release.

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Development