1

Is there a way to map property with database column with custom column, that IS NOT a FK, just a candidate key ( it is unique for table )?

If not, what is my options here? (need to restrict select results with joined table restrictions)

dbardakov
  • 651
  • 1
  • 8
  • 22

1 Answers1

1

NHibernate supports feature called property-ref. It is documented here: 5.1.10. many-to-one. Some extract:

The property-ref attribute should only be used for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key. This is an ugly relational model. For example, suppose the Product class had a unique serial number, that is not the primary key. (The unique attribute controls NHibernate's DDL generation with the SchemaExport tool.)

So, if the child table contains for example Guid, which is the same as in the target parent table... this could solve the issue. Example mapping:

<many-to-one name="Parent" property-ref="ParentGuid" column="THE_GUID_COLUMN"/>

Using the fluent syntax, it could look like this:

References(x => x.Parent)
    ...
    .PropertyRef("ParentGuid")
    .Column("THE_GUID_COLUMN");

Anyhow, this is not ideal and should be used mostly for solving legacy stuff.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • What is `property-ref` attribute counterpart in mapping-by-code? – dbardakov Dec 05 '13 at 13:17
  • 1
    As far as I know from this source: http://notherdev.blogspot.cz/2012/01/mapping-by-code-manytoone.html *"By now, there's no way to map two features useful for legacy inconsistent databases - property-ref and not-found. Maybe Fabio decided that legacy databases wouldn't be mapped with new mappings?"* – Radim Köhler Dec 05 '13 at 13:18
  • I read that after your answer, and was hoping the things might change since. – dbardakov Dec 05 '13 at 13:24
  • 1
    I see... sorry for that ;( but you could still be able to use another mapping as well. Anyhow, property-ref does exist, could be used (I did it few times)... but it is not "the best way" ... – Radim Köhler Dec 05 '13 at 13:26
  • Radim, I downloaded the newer version of NH 3.3.3.sp1 and at a glance looks like it has `property-ref` support. I will check this later. – dbardakov Dec 05 '13 at 14:43
  • That's promissing. This feature is part of the NHibernate core (I do prefer xml mapping...). The mapping by code is for sure very good option, because it is part of NHibernate distribution. Anyhow, property-ref is exactly what you need - if the referenced column is unique. – Radim Köhler Dec 05 '13 at 14:46