How to look up an NHibernate entity's table mapping from the type of the entity?
Asked Answered
R

2

8

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?

Rodrickrodrigez answered 11/3, 2010 at 23:55 Comment(0)
A
14

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
Attest answered 12/3, 2010 at 1:17 Comment(1)
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.Rodrickrodrigez
I
3

I have found this to work to get the name of the table where the entities are persisted.

  1. You have to have an instance of NHibernate.Cfg.Configuration
  2. You request an instance of NHibernate.Mapping.Table for the given persistent type.
  3. The Name property of the Table instance corresponds to the name of the the table where the entities are persisted.

See the code below.

NHibernate.Cfg.Configuration config = new Configuration();
/*
The initialisation here like config.AddAssembly(... and so forth
*/
NHibernate.Mapping.Table table = config.GetClassMapping(typeof(T)).RootTable;
String NameOfTableOfInterest = table.Name;

This could be wrapped in a function like so

public static String GetTableName<T>(NHibernate.Cfg.Configuration config)
{
    return config.GetClassMapping(typeof(T)).RootTable.Name;
}

NOTE: Strange enough, the properties Catalog and Schema of theNHibernate.Mapping.Table` have no value. At least, not in my case.

Isothere answered 18/10, 2013 at 10:41 Comment(1)
NHibernate stuff seem to all have old answers, at least for me. Anyway, this won't work if you have mapped an entity via entity-name, because GetClassMapping internally performs a dictionary look up. When mapping via class only, the said dictionary contains the full name of the type. But when mapping via entity-name, the dictionary contains the said entity name.Oxeyed

© 2022 - 2024 — McMap. All rights reserved.