Querying on an Any type mapping

56 views
Skip to first unread message

ccollie

unread,
Jan 10, 2009, 12:25:50 PM1/10/09
to nhusers
Im writing a generic taxonomy/tagging module where any entity in the
system can be tagged. The tagging table looks like

id int
UserId int
TaggedAt Date
Term varchar
ContentType varchar
ContentId int

To make this implementation easier (since i dont impose a base class
on the tagged entities), i want to use the Any type mapping:

<class name='SocialDance.Core.Domain.TaggedItem,
SocialDance.Core.Domain' table='`TaggedItem`'>
<id name='Id'>
<generator class='identity'/>
</id>

<many-to-one name="TaggedBy" class="SocialDance.Core.Domain.User,
SocialDance.Core.Domain" column="userId" not-null="false" />

<property name="Term" column="Term" type="String" length="50" />
<property name="TaggedAt" column="TaggedAt"/>

<any name='Item' meta-type='System.String' id-
type='System.Int32'>
<meta-value class='SocialDance.Core.Domain.Article,
SocialDance.Core.Domain' value='ARTICLE'/>
<meta-value class='SocialDance.Core.Domain.Photo,
SocialDance.Core.Domain' value='PHOTO'/>
<meta-value class='SocialDance.Core.Domain.BlogPost,
SocialDance.Core.Domain' value='POST'/>
<column name='ContentType'/>
<column name='ContentId'/>
</any>

</class>

My question is how do i query on the Any association ? I looked at
NHibernate's tests for the any mapping and they offer no clues. In
particular, i need to query for the tags on a particular item (seems
ok), and particular types (e.g. all photo tags for tag cloud calcs). I
assume that i cant query on any properties of TaggedItem.Item beyond
its type and id.


Thanks

Fabio Maulo

unread,
Jan 10, 2009, 1:49:21 PM1/10/09
to nhu...@googlegroups.com
You can do it only using an "implicit join" between TaggedItem and one of the <any> implementation
select a from TaggedItem ti, Article a where ti.Item.id = a.Id

Note the "ti.Item.id": the "id" property is a special "injected" property by NH. Using "id" in lowercase NH use the POID of the related entity.

2009/1/10 ccollie <gbo...@gmail.com>



--
Fabio Maulo

ccollie

unread,
Jan 10, 2009, 3:34:49 PM1/10/09
to nhusers
Fabio,
thanks, that covers the basics. But one further clarification. You're
saying that there is no way to do a Type predicate check or projection
on TaggedItem.Item
using either Hql or ICriteria ? For example, to get all types tagged
with a term if i dont know the type beforehand.


On Jan 10, 1:49 pm, "Fabio Maulo" <fabioma...@gmail.com> wrote:
> You can do it only using an "implicit join" between TaggedItem and one of
> the <any> implementationselect a from TaggedItem ti, Article a where

Fabio Maulo

unread,
Jan 10, 2009, 3:51:05 PM1/10/09
to nhu...@googlegroups.com
you can use another special "injected" property TaggedItem.Item.class if you want ask for a certain class.
In practice in HQL or Criteria you have only two available property for the <any> association:
TaggedItem.Item.id, TaggedItem.Item.class
Nothing more because NH can discover the type only loading a TaggedItem.
If you need to execute a query using a property of one of the associated entities you can do what we talk above:

select ti from TaggedItem ti, Article a, Photo p where (ti.Item.id = a.Id or ti.Item.id = p.Id) and ...... etc.

2009/1/10 ccollie <gbo...@gmail.com>



--
Fabio Maulo
Reply all
Reply to author
Forward
0 new messages