There is no way to mapping non-entity classes in JPA 1.
Since JPA 2.1, you can use ConstructorResult, Used in conjunction with the SqlResultSetMapping
annotation to map the SELECT clause of a SQL query to a constructor.
Here is the example
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)
}
)
}
)