-->
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: Native SQL Query in many-to-many
PostPosted: Tue Jun 28, 2005 5:03 am 
Newbie

Joined: Thu Apr 28, 2005 3:12 pm
Posts: 7
I have a class Person, which is associated with Person many-to-many associaton,
<hibernate-mapping>
<class
name="src.model.Person"
table="PERSONS"
>
.
.
.
<set
name="friends"
table="FRIENDS_PERSON"
lazy="true"
cascade="all"
sort="unsorted"
>

<key
column="HAVE_A_FRIEND"
>
</key>

<many-to-many
class="src.model.Person"
column="IS_A_FRIEND"
outer-join="auto"
/>

</set>
</hibernate-mapping>

I need to write a query which will check if given two person are friends. In native SQL this query would be something like this.

SELECT COUNT(*) FROM FRIENDS_PERSON WHERE have_a_friend=:id1 AND is_a_friend=:id2");

How to write it in Hibernate, or how to use this query as a native sql query in hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 6:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the native sql version is only possible in hibernate3 since it supports scalar return values.

examples for this are in the docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 7:13 am 
Newbie

Joined: Thu Apr 28, 2005 3:12 pm
Posts: 7
i have Hibernate3

in docs there are very simple examples. I cant find there how to retrieve values from tables which don't have counterparts in class model (such as in my above problem. I don't have class FriendPerson, and i need to execute some query on this table.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 7:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh ? your native sql does not need to refer to any entity...just map the return value as a <return-scalar> in hbm.xml or addScalar() in API.

its in the docs.

_________________
Max
Don't forget to rate


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.