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.  [ 8 posts ] 
Author Message
 Post subject: Mapping isue
PostPosted: Fri Aug 08, 2008 7:17 pm 
Newbie

Joined: Wed Feb 20, 2008 9:15 pm
Posts: 11
Hello,
i have a datastructure like this:
Image

I want a class with
the Contact.Name, Contact.Number, ContactLocationNumber.DialableNumber to a given Location.Name or given ContactLocationNumber.DialableNumber.

With pure SQL it's very easy with 2 joins, but how can I map such a scenario with nhibernate?

What is the way to go?
3 Mapping files each for one Database table and then join with hql?

Or is there another way without heavy using hql?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 5:41 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can map each entity and joni them with associations:

Code:
<class name="Contact">
  <bag name="ContactLocationNumbers">
     <one-to-many class="ContactLocationNumbers"/>
  </bag>
</class>

<class name="ContactLocationNumber">
   <many-to-one name "Location"/>
</class>


In HQL you can then use:

Code:
from Contact c where c.ContactLocationNumbers.Location.Name = "Somewhere"


Is that what you need ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 6:37 am 
Newbie

Joined: Wed Feb 20, 2008 9:15 pm
Posts: 11
yes, that looks great.

Is there also another way to do this with mapping files only?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 7:29 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
Is there also another way to do this with mapping files only?

I can't follow ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 12:55 pm 
Newbie

Joined: Wed Feb 20, 2008 9:15 pm
Posts: 11
Hello,
you HQL query results in a "expecting 'elements' or 'indices' after Location" Exception.

My SQL Query would look like
Quote:
select c.Name, c.Number, cls.DialableNumber from Contact c
join ContactLocationNumber cls on cls.ContactId=c.Id
join Location loc on cls.LocationId = loc.Id
where loc.Name='someloc'


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 7:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Then try this:

Code:
from Contact c
   join where c.ContactLocationNumbers cln
   join cln.Location l
   where l.Name = "Somewhere"

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 8:19 am 
Newbie

Joined: Wed Feb 20, 2008 9:15 pm
Posts: 11
Hello,
thank you. That's works.
However this results in (Contact.Count + 1 ) Queries against the Database. So for 50 Entries in the DB i got 51 queries.

Anyone any idea what's the probleme here?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 11, 2008 8:28 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look at the documentation:

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/queryhql.html#queryhql-joins

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/performance.html#performance-fetching

You can use a "fetch join" in HQL for retrieving collections with a join instead of single selects:

Code:
select c.Name, c.Number, cls.DialableNumber from Contact c
join fetch ContactLocationNumber cls on cls.ContactId=c.Id
join Location loc on cls.LocationId = loc.Id
where loc.Name='someloc'

_________________
--Wolfgang


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