0

In EclipseLink 2.5 (JPA 2.1), I'm trying to map the results of a native query to a POJO, which according to these links should be possible:

...using this syntax (taken straight from EclipseLink 2.5 api docs):

Query q = em.createNativeQuery(
      "SELECT c.id, c.name, COUNT(o) as orderCount, AVG(o.price) AS avgOrder " +
      "FROM Customer c, Orders o " +
      "WHERE o.cid = c.id " +
      "GROUP BY c.id, c.name",
      "CustomerDetailsResult");

@SqlResultSetMapping(
       name="CustomerDetailsResult",
       classes={
          @ConstructorResult(
               targetClass=com.acme.CustomerDetails.class,
                 columns={
                    @ColumnResult(name="id"),
                    @ColumnResult(name="name"),
                    @ColumnResult(name="orderCount"),
                    @ColumnResult(name="avgOrder", type=Double.class)
                    }
          )
       }
      )

However, the "classes" attribute of the @SqlResultSetMapping is not found. I tried with Eclipselink 2.5.2, and 2.6. Is there another (optional) EclipseLink jar I must use to get that functionality?

user497587
  • 45
  • 9
  • Looks like this guide is outdated, try http://docs.oracle.com/javaee/7/api/javax/persistence/SqlResultSetMapping.html – Petros Splinakis Mar 27 '15 at 17:46
  • How can you tell it's outdated? My link is for EclipseLink 2.5. Your link doesn't even say which implementation or version of JPA it's for. And besides, both links indicate that the "classes" property of SqlResultSetMapping exists, so I'm not sure what your suggestion is. – user497587 Mar 28 '15 at 01:20
  • Sometimes people mistakenly annotate the POJO with `@SqlResultSetMapping`. Please see this http://stackoverflow.com/a/25202635/3796586 and tell me if that's your case. – zbig Mar 28 '15 at 22:57
  • 2
    are you sure you imported javax.persistence.SqlResultSetMapping not some other annotation. I just tried with your code as it is and it worked. – Zielu Mar 29 '15 at 13:46
  • Could you be using the JPA 1 jar on your classpath? – Chris Mar 30 '15 at 13:41
  • @zbig You stated in another question that, "Mapping to POJO class with @ ConstructorResult was added in version 2.1 of JPA. POJO used with the mapping has to have correct constructor.". That's exactly what I'm trying to do. Here is your answer that I'm quoting: http://stackoverflow.com/questions/25179180/jpa-joining-two-tables-in-non-entity-class/25184489#25184489 – user497587 Mar 31 '15 at 17:32
  • @Chris I'm am using the play framework, and importing eclipselink 2.5.2, which is based on JPA 2.1. Here's the config line from my Play framework build.sbt: "org.eclipse.persistence" % "eclipselink" % "2.5.2", – user497587 Mar 31 '15 at 17:33
  • Eclipselink implements the 2.1 spec, but your issue is with the javax.persistence.SqlResultSetMapping class - You still need to be using the JPA 2.1 jar. The version you have must be an earlier version of the JPA jar. The persistence jar includes all the interfaces within the specification, such as SqlResultSetMapping, while the EclipseLink and other provider jars include backward compatible implementation classes. – Chris Mar 31 '15 at 18:37
  • Thanks @Chris, that's very good to know. It looks like [this SO answer](http://stackoverflow.com/questions/25055469/where-do-the-dependencies-javaws-javajpa-etc-come-from-in-play-java-applicati) might be related to my problem. Even though I'm using EclipseLink 2.5.2 library, maybe the JPA jar that is provided by JavaJPA in build.sbt implements and earlier JPA spec. But I don't have enough reputation to comment on that conversation. – user497587 Apr 01 '15 at 12:38
  • I don't quite understand how you are getting your jars. You need to include javax.persistence_2.1.0.v201304241213.jar within the EclipseLink installer jar on your classpath (taken from the EL 2.6 install bundle) before other persistence jars. – Chris Apr 01 '15 at 14:57

0 Answers0