Hi, I try to fetch objects with many-to-many relation, but I don’t really know how to do that
I use nhibernate since view days and try to understand and use it for my current project.
I use
NHibernate-2.0.0 with
MS-SQL 2005, here are my files
First class, is a main class contains needed data.
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MediaLibrary.Core.Content, MediaLibrary.Core" table="CONTENTS" lazy="false">
<id name="Id" column="id" type="int">
<generator class="native" />
</id>
<property name="Path" column="path" type="String" length="1024"/>
<property name="Rating" column="rating" type="int"/>
<bag name="Keywords" table="CONTENTS_KEYWORDS" cascade="all">
<key column="contentid"/>
<many-to-many column="keywordid"
class="MediaLibrary.Core.Keyword, MediaLibrary.Core"/>
</bag>
</class>
</hibernate-mapping>
And here the second contains Keywords (Tags) to find a content class
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MediaLibrary.Core.Keyword, MediaLibrary.Core" table="KEYWORDS" lazy="false">
<id name="Id" column="id" type="int">
<generator class="native" />
</id>
<property name="Value" column="value" type="String"/>
</class>
</hibernate-mapping>
Here is my many-to-many relation in sql

I try to execute this query
Code:
SELECT CONTENTS.*
FROM CONTENTS_KEYWORDS INNER JOIN
KEYWORDS ON CONTENTS_KEYWORDS.keywordid = KEYWORDS.id INNER JOIN
CONTENTS ON CONTENTS_KEYWORDS.contentid = CONTENTS.id
WHERE (KEYWORDS.Value LIKE 'foo' OR KEYWORDS.Value LIKE 'bar' OR ... )
Which way should I prefer? Criteria, HQL or SQL?
I tried to create a criteria because I have to build where statement dynamically
Code:
DetachedCriteria query = DetachedCriteria.For(typeof(Keyword));
Disjunction disj = new Disjunction();
foreach (string key in keywords)
disj.Add(Expression.Like("Value", key));
query.Add(disj);
ICriteria crit = SessionProvider.Default.Session.CreateCriteria(typeof(Content));
// .... ???
I will be happy with any help guys