-->
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.  [ 2 posts ] 
Author Message
 Post subject: Many-to-many using link table with value
PostPosted: Mon Mar 16, 2009 11:13 pm 
Newbie

Joined: Mon Mar 16, 2009 10:51 pm
Posts: 3
Location: Atlanta
I have been searching far and wide and seem to find many examples that are "close" to what I need, but not exactly.

To keep it simple, I have this:

Class Article
Key ID
Property int Views
List<KeywordInstance> KeywordInstances

Class KeywordInstance
Key ID
Property Article InstanceArticle
Property Keyword Keyword
Property int Weight

Class Keyword
Key ID
List<KeywordInstance> KeywordInstances

Again, over simplified, but I have articles, and I have keywords. Then there is a link table that maps articles to keywords (many to many) with a weight (i.e. frequency the keyword appears in the article).

I have two-way navigation. An article should give me its list of keywords that I will sort by weight, and a keyword should give me its list of articles that I can also sort by weight.

These are my mapping tables (simplified for space, the key parts I believe are there)

<class
name="Article"
lazy="true">
<id
name="ID"
>
<generator
class="native"/>
</id>
<property name="views"/>
<set
name="KeywordInstances"
cascade="all"
inverse="true"
>
<key
column="ArticleID"/>
<one-to-many class="KeywordInstance"/>
</set>
</class>

<class
name="Keyword"
lazy="true"
>
<id
name="ID"
<generator
class="native"/>
</id>
<set
name="KeywordInstances"
cascade="all"
inverse="true"
>
<key
column="KeywordID"/>
<one-to-many class="KeywordInstance"/>
</set>
</class>

<class
name="KeywordInstance"
lazy="true"
>
<id
name="ID"
>
<generator
class="native"/>
</id>
<property
name="Weight"/>
<many-to-one
name="InstanceArticle"
column="ArticleID"
class="Article"
cascade="none"
not-null="true"/>
<many-to-one
name="InstanceKeyword"
column="KeywordID"
class="Keyword"
cascade="none"
not-null="true"/>
</class>

The typical cycle is to pull in an article, roll through it's keywords to generate the meta-tags, and then update a "view count" - literally, something like this:

Article article = _session.Select(typeof(article),key);
foreach(keywordinstance key in article.keywordinstances)
{
// do something with key.InstanceKeyword.KeywordValue
}
article.Views++
_session.flush();

The problem that I'm having is that a simple display of a single page suddenly graphs for me hundreds of select statements over keyword, even if it only has two keywords.

I very this with the profiler. Any thoughts on where I might be going wrong?


Top
 Profile  
 
 Post subject: Solved my own issue, but you might enjoy the answer ..
PostPosted: Thu Mar 19, 2009 5:43 pm 
Newbie

Joined: Mon Mar 16, 2009 10:51 pm
Posts: 3
Location: Atlanta
So the first mistake is probably obvious to others but wasn't so obvious for me. I was trying to take a <set> and map it to an IList<T> ... this was through an access violation. So I build a proxy, and set collection-type = my proxy and hydrated the list. This blew my chances of using a cache and forced an invalid mapping (i.e. stuffing a set into a list, etc).

So the solution was of course to do a bag - I wanted set-like functionality but don't want to dirty my domain with the ISet so I do a bag and then enforce uniqueness elsewhere.

Here's the catch, though - I found it using NHProf. (NHibernate Profiler). I have to give major thumbs up there ... I downloaded the trial and found my issue in about 3 minutes. The profiler is incredible, lots of great information especially seeing the call stack and tracing to the exact property access that triggers the proxies to go out and fetch data.

Anyway, thanks and I hope this helps save someone else the frustration I experienced. Mapping Bag to IList means no excessive N+1 queries now and complete used of the 2nd level cache which NHProfiler demonstrated provides extensive speed benefits when you are a mostly read (not read/update all the time) site.


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