Search code examples
c#nhibernatefluent-nhibernateauto-incrementcomposite-id

FluentMapping ComposedId with AutoInc


I am creating a transfer data tool for the company application, to move data from the old version to the new, and back. Since now we are using NHibernate and FluentMapping, I'll have to map all old tables, and there is a problem with one table with composed primary key. The table looks like this:

public class OldTable {
 public string KeyOne {get;set}
 public string KeyTwo {get;set}
 public string KeyN {get;set}
 public int Sequence {get;set}
 ...
}

And I need to map all the above fields as a composite key, the only problem is that "Sequence" increments based on the other fields to avoid duplicated keys, the others are a mix of foreign keys and simple strings. Is there a way to do this mapping? If it takes creating a expression to calculate the Sequence, no problem.

The good news is the table in the new system just uses a integer Id.


Solution

  • From the documentation:

    You can't use an IIdentifierGenerator to generate composite keys. Instead the application must assign its own identifiers.

    You see this in the HBM XML schema, too. <id> allows a <generator> to be specified, but <composite-id> does not.

    That's not to say this feature couldn't be added to NHibernate. If this is something that would be useful to you, please submit your feature request to the NHibernate issue tracker. Your feature request is most likely to be successful if this feature has been implemented in Hibernate first - if so, it may be simple to port it over. It sounds like it may be implemented in Hibernate already: HHH-2060