override NHibernate auto mapping config

Topics: Writing modules
Feb 24, 2011 at 5:18 PM

Hi, I'm trying to write simple module for storing parent-child relations between the tag records. My goal is simple: build hierarchical structure. Let's say I have a table with these columns:


And here is my problem. How can I create two one-to-one relations where each of them points to the same class (TagRecord)? Orchard how-to says:

"It is important here that the name of the column that will represent the relation is the name of the column on the "1" end of the relation, followed by an underscore and the name of the column of the "n" end of the relation."

It's obvious that I can't have two columns with the same name :) so after some searching I found this discussion on stackoverflow.  The thing can be (hopefully) solved by overriding the default auto mappings like this piece of code shows:

   return Fluently.Configure()
    .Mappings(m => m.AutoMappings.Add(
            .Override<User>(map=> map.HasMany(user=> user.Transactions).KeyColumn("Buyer_id"))
            .Override<User>(map => map.HasMany(user => user.Transactions).KeyColumn("Seller_id"))                

So is there any way how to apply this code in my module (e.g. in my service)? Please can you tell me how can I obtain especially these two objects: Fluently and persistenceConfigurer from the deeps of Orchard? I'm new to NHibernate and Orchard and it could save me a lot of hours only to figuring out if it is even possible.

Thanks in advance.

Feb 24, 2011 at 6:30 PM

I'll try to summon someone who would be able to answer that question but before I do, I would ask a couple of questions:

1. Did you consider other and more efficient hierarchical data structures than simple parent references, such as interval trees?

2. It seems like you're building a hierarchy of tags. Did you look at the Taxonomy module, which does that already?

Feb 24, 2011 at 7:37 PM

Thank you for your reply.

About your questions:

1. No, I did not consider any other data structure. It was supposed to be "how this NHibernate and Orhcard system works" learning example and I wanted to keep it as simple as possible. No fancy stuff around, just create something that "extends" another module to see how the things works. But there is, of course, a huge amount of space for improvements :)

2. Thanks for the tip, it looks interesting.

But this is not about hierarchy of tags anymore. The man from discussion above wanted to know how he can create two relations on the same class. It's a perfect example. He has an item with two properties Buyer and Seller. Let's assume that both of them could be classes of UserPartRerdord. I think this is the same case and I would like to know how to do it.

Feb 24, 2011 at 7:47 PM

Ah, OK. Well, it's the consistency of the column name and property name that counts if I'm not mistaken.

Feb 24, 2011 at 8:12 PM

Yes! Sorry if my expression isn't clear enough. It's a fight to write even few lines in English. I would like to create somethig like this:

public virtual int Id { get; set; }
public virtual TagRecord Parent { get; set; }
public virtual TagRecord Tag { get; set; }


Feb 24, 2011 at 9:40 PM

You should be fine with just making sure the name of the column is the name of the property plus _id. That should enable you to have the same table having two links to another.

Jul 2, 2012 at 11:38 AM
Edited Jul 2, 2012 at 11:39 AM

Hi! How can i extend this with a cascading delete functionality? - delete all child items on parent deletion. Is there a way to get this working?

public virtual IEnumerable<Tag> Children  { get; set }
Jul 3, 2012 at 2:23 AM

If you look at the TagService class, you'll see that it's done by deleting each record individually. It doesn't do cascaded deletes because the relationships are not physically enforced.

Jul 3, 2012 at 7:20 AM
Edited Jul 3, 2012 at 7:22 AM

I am looking at the TagService, but i don't see any hierarchy there. I am trying to create a simple tree structure of non-content items.

Like this:

public class NodeRecord
    public virtual int Id { get; set; }
    public virtual string NodeName {get; set;}   
    public virtual NodeRecord Parent { get; set; }
    //problem to get this work (automapping to a non-existing table named NodeRecordToNodeRecord)
    public virtual IEnumerable Children{ get; set; }

Is there a way to get this to work a "nice way"? I can get it work by manually deleting items in a service implementation, as you sugest, but i think this should be possible to let NHibernate do that for us.

- And what i am really trying to do is a new menuitem (my code is based on 1.x) wich dynamically injects a subtree, like querymenuitem does with only one level, but with a hierarchical structure in mind.

OT: Is there a supported way to add localization to non-content record? What would you suggest?

Thank you very much for your help.

Jul 4, 2012 at 5:27 AM

I'm not saying it's a hierarchy, I'm just pointing you to code that does deletes of related items. Yes, nHibernate supports cascaded deletes, but there must be a physical relationship for it to work, one that the nHibernate mapping knows about.

No, there is no supported way to add localization to non-content records, you are on your own for that one.