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.  [ 4 posts ] 
Author Message
 Post subject: HQL MANY TO MANY
PostPosted: Fri Mar 07, 2008 10:28 pm 
Newbie

Joined: Fri Mar 07, 2008 10:01 pm
Posts: 3
Hi,

I need to do this HQL:

i have 2 tables: Project(ProjectId, Name) and Users(userId, Name) and a Many to Many relation.
1 project have many users and 1 user have many projects.
this results in a other table: ProjectUser(id, ProjectId, userId)
this structure already is mounted in the Active Record and nHibernate.

i'm using Active Record with nHibernate and im trying to verify if a user is a project's member. I try with ExecuteScalar(@"Select COUNT(*) From ... Join ...); but this dont work...

I need Help please!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 08, 2008 4:43 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you be a bit more specific about your problem ? Classes, code for your hql query ... ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 08, 2008 6:21 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try something like

"select count(*) from Project p inner join p.Users
where ? in elements(p.Users)"

and check if the result set is greater 0 (you may get mor than 1 result due to the join which results in a project for each user in that project). But for me that does not look like the best approach. Can you post more information ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 11:06 am 
Newbie

Joined: Fri Mar 07, 2008 10:01 pm
Posts: 3
works fine!!!!

thanks!

it's the method:

public bool VerificaVinculoUsuario(Projeto projeto, Usuario usuario)
{
string sql = "select count(*) from Projeto p inner join p.UsuariosAssociados where ? in elements(p.UsuariosAssociados) and p = ?";
return ExecuteScalar<long>(sql, usuario, projeto) > 0;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.