@ConstructorResult的情况下将@SqlResultSetMapping放在哪里 [英] Where to place @SqlResultSetMapping in case of @ConstructorResult
问题描述
我正在尝试使用jpa的EntityManager的createNativeQuery方法映射非实体pojo.通过使用这样的东西
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;
}
}
我收到unknown SqlResultSetMapping ResultMapping
错误
我应该将SqlResultSetMapping放在哪里,以便EntityManager能够识别它?
I getting unknown SqlResultSetMapping ResultMapping
error
Where I am supposed to put SqlResultSetMapping so that the entityManager will able to recognize it?
推荐答案
我应该将SqlResultSetMapping放在哪里,以便EntityManager能够识别它?
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 :将其放在类路径中的任何类上
-
Hibernate :将其放在任何带@Entity注释的类中;实际上,当我将其放在其他位置时,我能够重现该错误:
- 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]
在EclipseLink 2.5.2,Hibernate 4.3.8.Final上进行了测试
Tested with EclipseLink 2.5.2, Hibernate 4.3.8.Final
通常,为了使您的应用程序可跨JPA提供程序移植,最好将SqlResultSetMapping放在任何实体类中.
In general to make your application portable across JPA providers it would be the best to put SqlResultSetMapping at any entity class.
这篇关于@ConstructorResult的情况下将@SqlResultSetMapping放在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!