Domanda

La mia mappatura HibernateUserImpl appare come di seguito

<set name="groups" table="OS_USER_GROUP" inverse="false" cascade="none" lazy="false">
    <key column="user_id"/>
    <many-to-many column="group_id" class="com.opensymphony.user.provider.hibernate3.ahxu.impl.HibernateGroupImpl"/>
</set>

Faccio query HQL come

select distinct hibuser from HibernateUserImpl hibuser, hibuser.groups.elements  hibgroup where hibuser.name = ? and hibgroup.name = ?

Ricevo un errore per hibuser.groups. elementi . C'è qualcosa che mi manca?

errore nella console

org.hibernate.hql.ast.QuerySyntaxException: hibuser.groups.elements is not mappe
d [select distinct hibuser from com.opensymphony.user.provider.hibernate3.ahxu.i
mpl.HibernateUserImpl hibuser, hibuser.groups.elements  hibgroup where hibuser.n
ame = ? and hibgroup.name = ?]
È stato utile?

Soluzione

Penso che tu debba fare un join .

esplicito
    select distinct hibuser 
    from HibernateUserImpl hibuser
    join hibuser.groups hibgroup 
    where hibuser.name = ? and hibgroup.name = ?

Altri suggerimenti

hibuser.groups è una raccolta, pertanto non è possibile accedere all'attributo dell'elemento della raccolta.

Questo può essere illustrato come:

Elenco gruppi = ...; groups.elements < - accesso alle funzioni illegali.

Pertanto, per accedere a hibuser.groups.elements , è necessario eseguire più join:

select element 
from HibernateUserImpl hibuser 
join hibuser.groups group 
join group.elements element 
where hibuser.name = ? and hibgroup.name = ? and element.whatever = something

Ma qui selezioni solo su hibuser, quindi non dovresti unirti ad altre raccolte. Non aderire ti darà prima una query più veloce ed eviterà l'uso di distinti. Cerca di evitare il più distinto possibile.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top