views:

45

answers:

1

Once I've mapped my domain in NHibernate, how can I reverse lookup those mappings somewhere else in my code?

Example:

The entity Pony is mapped to a table named "AAZF1203" for some reason. (Stupid legacy database table names!) I want to find out that table name from the NH mappings using only the typeof(Pony) because I have to write a query elsewhere.

How can I make the following test pass?

private const string LegacyPonyTableName = "AAZF1203";

[Test]
public void MakeSureThatThePonyEntityIsMappedToCorrectTable()
{
    string ponyTable = GetNHibernateTableMappingFor(typeof(Pony));
    Assert.AreEqual(LegacyPonyTableName, ponyTable);
}

In other words, what does the GetNHibernateTableMappingFor(Type t) need to look like?

+4  A: 

At which point do you need that information?

Because it depends on what you have...

Not long ago I had to get the table name from an audit event listener, and I used this:

IPostDatabaseOperationEventArgs args //parameter
var tableName = ((ILockable)args.Persister).RootTableName.ToLower();

You could also get it from the session...

((ILockable)session.GetSessionImplementation()
                   .GetEntityPersister(null, new Pony())).RootTableName
Diego Mijelshon
Beautiful! I used the first operation you mentioned because I am trying to pull this out in a SaveEventListener. I can pull the persister from the eventArgs there. I didn't realize that I had to cast it to ILockable. Works perfectly.
snicker