Logo

NHibernate

The object-relational mapper for .NET

Using convention to resolve sequence names

I’m currently working in a project with a legacy database. The database use a convention, where every table has his own identifier (one-column) and it must be generated by a sequence.

The name of the sequence follows a convention, first 26 characters of the Table name + the subfix “seq”. For instance, the sequence for the table Nationality is NATIONALITY_SEQ.

I take advantage of the convention as follows:

  • I’ve mapped everything as “native”.
  • I wrote a new id generator as follows:
public class SequenceByConvention : SequenceGenerator
{
    public override void Configure(IType type, IDictionary<string, string> parms, Dialect dialect)
    {
        parms["sequence"] = GetSequenceNameFromTableName(parms["target_table"]);
        base.Configure(type, parms, dialect);
    }
    private static string GetSequenceNameFromTableName(string tableName)
    {
        return tableName.Substring(0, Math.Min(26, tableName.Length)) + "_SEQ";
    }
}
  • And finally this is the dialect for this project:
public class MyDialect : Oracle10gDialect
{
    public override System.Type NativeIdentifierGeneratorClass
    {
        get { return typeof (SequenceByConvention); }
    }
}

Posted Mon, 13 December 2010 02:03:00 PM by jfromainello
Filed under: identifier

comments powered by Disqus
© NHibernate Community 2024