I am using Hibernate, and I'm trying to return some info about users which are saved in 2 separate tables.
The query I've made:
@Entity(name = "Users")
@Table(name = "Users")
@NamedQueries(
{
@NamedQuery(name = QueryNames.QUERY_USER_GET_ALL_USERS_BASIC_INFO,
query = "select u.userId, p.personName, p.personMobile, p.personEmail, u.userStaus, u.usertype, p.personNotes "
+ "FROM Person p JOIN User u ON p.personId = u.userPersonId "
+ "Where u.Active = 1")
})
public class User
{ ... }
I have made a class called BasicUserInfo
to contain all the members I return in the select clause (just a class with members only and no method for Json purposes).
The way I'm trying to make it work is like this:
List<BasicUserInfo> list = list(namedQuery(QueryNames.QUERY_USER_GET_ALL_USERS_BASIC_INFO));
However, this does not compile since the last line with the activation above got an error:
Type mismatch: cannot convert from
List<User>
toList<UserBasicInfo>
What can I do to make the query return the type of list I want? Or even just a list of Object[]
is also good for Json purposes.
What I eventually did was changing the query to begin like this:
"select new com.myPackage.Users.UserBasicInfo(u.userId, p.personName, p.personMobile, p.personEmail, u.userStaus, u.usertype, p.personNotes) ...
The type needs to have a constructor which accepts the arguments returned by the query.