I have two tables: User and Roles. and User is mapped to roles using a many to many relationship.
what i want, is to get a selection of columns including the ones in role. So I created the following named query:
SELECT u.username, u.password, u.salt, u.enabled, u.roles FROM User u WHERE u.username = :username
However Hibernate is failing to compile, and the SQL log is the following:
Hibernate:
select
user0_.username as col_0_0_,
user0_.password as col_1_0_,
user0_.salt as col_2_0_,
user0_.enabled as col_3_0_,
. as col_4_0_,
role2_.id as id42_,
role2_.friendly_name as friendly2_42_,
role2_.name as name42_
from
users user0_
inner join
users_roles roles1_
on user0_.id=roles1_.users_id
inner join
roles role2_
on roles1_.roles_id=role2_.id
where
user0_.username=?
what intrigues me is . as col_4_0_, ! It is causing the problem, but why would hibernate include it, and how to fix this issue?
Many Thanks
Either query for User instances, and fecth the roles with them:
Or use a query that returns scalars, but then you can’t just treat the collection af roles as if it were a scalar:
Side note: it should be
userName, notusername, to respect the standard Java naming conventions.