-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: HQL simple sur une Collection
PostPosted: Fri Jan 20, 2006 8:25 am 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
Bonjour,

Voila, j'ai une requête qui en SQL est assez simple mais j'ai pas clairement compris comment bien faire en HQL.

en SQL :
Code:
select distinct user_id from GROUP_MEMBERS where group_id = ?


bien sur, j'aimerais récupérer le 'primaryKey' de chaque utilisateur appartenant au groupe.

Hibernate version: 3.1

Mapping documents:

Code:
<class name="UserBMP" table="USERS" node="user">
<id name="primaryKey" column="user_id" type="java.lang.String" node="@user_id" unsaved-value="null"/>
...
<set name="groups" table="GROUP_MEMBERS" fetch="select" lazy="true" embed-xml="false" cascade="persist,save-update" inverse="true">
<key column="user_id"/>
<many-to-many column="group_id" class="GroupBMP" embed-xml="false" fetch="select"/>
</set>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 10:35 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Ton lien est marqué comme inverse donc tu devrais pouvoir simplement faire
Code:
monGroupe.getUsers();

sinon le hql doit être du genre
Code:
select distinct user.id from UserBMP user where user.groups.id=:group_id

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 11:25 am 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
Merci

Il est vrai que je ne veux pas deployer hibernate sur le client donc ne pas utiliser de méthodes distantes utilisant les collections persistantes mais que je peux toujours les utiliser sur mes facades.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 11:56 am 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
J'ai bien :

Code:
select distinct U.id from UserBMP U where U.groups.id = ?


Par contre, la valeur de retour doit pas être correcte car il me trace :

Quote:
java.lang.ClassCastException: $Proxy261
at org.hibernate.type.StringType.toString(StringType.java:44)
at org.hibernate.type.NullableType.toLoggableString(NullableType.java:169)
at org.hibernate.pretty.Printer.toString(Printer.java:65)
at org.hibernate.engine.QueryParameters.traceParameters(QueryParameters.java:256)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:149)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1127)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at AbstractBean.findBy(AbstractBean.java:156)
at UserBean.ejbFindByGroup(UserBean.java:248)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 12:15 pm 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
D'après la stackTrace c'est le paramètre que tu passe qui est incorrect alors soit tu fais un "where U.groups=?" et tu passes l'instance de groupe soit tu fais "where U.groups.id=?" et tu passes l'id. Il semble que tu fasses la deuxième requête avec l'instance en paramètre

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 12:39 pm 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
Exact. Mon paramétrage était incorrect mais ca plante toujours...

javax.transaction.TransactionRolledbackException: findBy error : user.findByGroup; CausedByException is:
a different object with the same identifier value was already associated with the session: [UserBMP#Utilisateur3]; nested exception is:
javax.ejb.EJBException: findBy error : user.findByGroup; CausedByException is:
a different object with the same identifier value was already associated with the session: [UserBMP#Utilisateur3]
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:259)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:335)
at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:146)
at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.java:130)
at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:121)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invokeHome(ProxyFactoryFinderInterceptor.java:93)
at org.jboss.ejb.EntityContainer.internalInvokeHome(EntityContainer.java:508)
at org.jboss.ejb.Container.invoke(Container.java:894)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 22, 2006 9:59 am 
Regular
Regular

Joined: Wed Sep 21, 2005 1:37 pm
Posts: 53
Location: Montpellier - France
J'ai changé le mapping en utilisant la clé via 'element' et donc, sans utiliser le 'many-to-many'.

Et pour cela, ca marche bien.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.