Reputation: 3536
I'm using the select new syntax to query a non-entity in the database and get a not mapped error...
The object (not an Entity):
package user;
public class User {
private int id;
private String lastName;
private String firstName;
private String mail;
}
The table (P042USER) is created like this:
CREATE TABLE P042USER (id INT PRIMARY KEY NOT NULL, lastname VARCHAR(100), firstname VARCHAR(100), email VARCHAR(255));
Then i want to do a request with the select new syntax:
Query q = em.createQuery("SELECT NEW user.User(u.id, u.lastname, u.firstname, u.email) FROM P042USER u ORDER BY u.id");
return q.getResultList();
Finally i got the error:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: P042USER is not mapped
Of course it is not mapped, that's why I use the select new syntax... Have you got any idea? Thanks.
Upvotes: 0
Views: 861
Reputation: 4154
Alternatively, you can modify your User
class to be an entity (annotated by @Entity
) that can be mapped to a specific table other than your P042USER
table. For example, you map it to table named ANOTHER_USER
. When you create new User entities, they can be persisted to your ANOTHER_USER
. At the same time, when you execute native SQL queries against your P042USER
table, results of your native SQL can be mapped to the User
class.
A sample of this implementation can be seen in my github repo.
Upvotes: 0
Reputation: 603
In JPQL you work with entities. In your case what you need is a native query and SqlResultSetMapping. Check this ConstructorResult JavaDoc for an example.
Upvotes: 1