Powerslave
Powerslave

Reputation: 457

NHibernate: many-to-one without primary key

Given two tables:

Table A with Columns Id (PK) and Name

Table B with Columns Id (PK) and Name (of A)

I would like to have B as a property of A, associated by Name which is no primary key.

How would I map (many-to-one) that in A.hbm.xml if its possible?

Thank you.

Upvotes: 2

Views: 1165

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123861

we can use magical property-ref setting:

on parent side (class A), collection mapping:

<bag name="Children" lazy="true" inverse="true" 
     batch-size="25" cascade="all-delete-orphan" >
  <key column="Name" property-ref="Name" />
  <one-to-many class="B"/>
</bag>

and child (class B) can reference parent similar way:

<many-to-one not-null="true" name="Parent" class="A"
   property-ref="Name" column="Name"  />

check also: https://stackoverflow.com/a/31300425

A link to doc: 5.1.11. many-to-one

... property-ref: (optional) The name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

Upvotes: 2

Related Questions