Obscure N/Hibernate Feature: <any> & <many-to-any>

time to read 2 min | 384 words

NHibernate & Hibernate has a really cool feature by which you can map two columns in a database to any arbitary object. The first column is the type of the the object you want to create, and the second is the id of the object you want to create. Unsurprisingly, this feature is called <any> / <many-to-any>. It's recommended that you wouldn't use it since it's not very OO, but sometimes there isn't much choice.

I run into such a situation recently. I've a set of rules that can be applied to all sorts of objects. Thus, I need to use <any> and <many-to-any>. The <any> feature is relatively easy to understand, and I'd it implemented and (probably) working in a couple of hours. But allows me to repreduce in full the documentation there is for the <many-to-any> and the <index-many-to-any> in Hibernate (the mother project, the one with all the documentation).

 The <many-to-any> and <index-many-to-any> elements provide for true heterogeneous associations. These mapping elements work in the same way as the <any> element - and should also be used rarely, if ever.

They really don't want you to use it, appernatly. No worries, I thought, I'll just go to Hibernate In Action and get the information from there.

Buzz! Wrong! Go sit in the corner!

No mention of anything useful about it (but tons of other useful stuff, of course). I'm reduced to reading XML Schemas and DTD, trying to understand what is going on.

I'll post again when I've the answer. I asked in the forum and got an answer from Sergey (the lead developer of NHibernate), so I hope to get real info before I've to start debugging NHiberante again.

If someone who reads this ever used this feature in NHibernate or Hibernate, I would love to hear from you. I just don't know how it's supposed to work, and the results I'm getting from prodding in the dark are painful.