Hi,
I have some problems in finding the correct HQL statement for an actual easy query. I have an object "User" with some attributes like username, password and with a Set of aliases that are mapped as as CollectionOfElements. The mapping looks like
Code:
public class User {
private String username;
private String password;
private Set<String> aliases = new HashSet<String>();
@CollectionOfElements(fetch = FetchType.EAGER)
@JoinTable(name = "USER_ALIAS", joinColumns = @JoinColumn(name = "user_id"))
public Set<String> getAliases() {
return aliases;
}
Now I want to write a HQL query that returns the user with a given alias name (all aliases are unique). I tried this one
Code:
final String queryString = "from User u where :alias in elements(u.aliases)";
final List<User> result = getHibernateTemplate().findByNamedParam(queryString,"aliases", "myAliasesToLookFor");
but the result list is empty. If I let Hibernate generate the sql statement it is
Code:
select
user.id,
user.password,
user.username
from
USERTABLE user
where
? in (
select
aliases.alias
from
USER_ALIAS aliases
where
user.id=aliases.user_id
)
and if I run that against my database directly I get the correct user.
Can someone explain to me what is wrong with my HQL?
Thanks in advance,
Ole