14

I'm trying to map a non-entity pojo with the createNativeQuery method of entityManager of jpa. By using something like this

@SqlResultSetMapping(name="ResultMapping", 
classes={
@ConstructorResult(
     targetClass=Employee.class,
       columns={
          @ColumnResult(name="empID", type=Long.class),
          @ColumnResult(name="empName", type=String.class),
          }
   )
}
)
public class Loader{
 private EntityManager em;

 public void load(){

   Query query = em.createNativeQuery("select empID, empName from employee",  "ResultMapping");
   List<Employee> = query.getResultList();
 }

}

public class Employee{

 private long empID;
 private String empName;
 public Employee(long empid, String empname)
 {
     this.empID = empid;
     this.empName = empname;
 }
}

I getting unknown SqlResultSetMapping ResultMapping error Where I am supposed to put SqlResultSetMapping so that the entityManager will able to recognize it?

1 Answer 1

23

Where I am supposed to put SqlResultSetMapping so that the entityManager will able to recognize it?

As far as I can see it varies from a persistence provider:

  • EclipseLink: put it at any class in the classpath
  • Hibernate: put it at any class annotated with @Entity; in fact I am able to reproduce the error when I put it elsewhere:

    org.hibernate.MappingException: Unknown SqlResultSetMapping [ResultMapping]
    

Tested with EclipseLink 2.5.2, Hibernate 4.3.8.Final


In general to make your application portable across JPA providers it would be the best to put SqlResultSetMapping at any entity class.

Sign up to request clarification or add additional context in comments.

4 Comments

JPA is specification while EclipseLink, Hibernate, OpenJPA, DataNucleus and others are its implementations (persistence providers). There is no such thing as 'pure JPA' because specification does not precise how a given persistence provider should behave in this particular case, although it might be specified in the future.
put it at any class annotated with @Entity Is there any widely used/ known convention of putting them somewhere. I have a bunch of queries and I do not want to pollute an entity with these. Also, my Queries are some Reporting use case queries and have no such strong connection with any single.
@krozaine if you don’t want to pollute your entities with annotation blocks, you can define them in an XML mapping file with sql-result-set-mapping block.
it makes sense since you are mapping something retrieved from database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.