4

The legacy database I've inherited contains the following tables:

Teams ( 
 TeamId INT PRIMARY KEY,
 Name VARCHAR(30)
)

Players (
 PlayerId INT PRIMARY KEY,
 Team VARCHAR(30)
)

The foreign key in the players table refers to the team name, rather than teamId.

I've attempted to map from Team to Players using a bag:

<bag name="Players">
    <key column="Team" foreign-key="Name" />
    <one-to-many class="DataTransfer.Player, DataTransfer" />
</bag>

But I get SqlException: Conversion failed when converting the varchar value 'Arsenal' to data type int

I've been able to use a bag to map string foreign keys in other areas, but in those cases the foreign key referred to the primary key of the parent table.

Edit: I'm using NHibernate 2.0.1

Mr Plough
  • 211
  • 1
  • 3
  • 11
  • Hi, We are having the same problem, have you found any solution to this? – fredrik Apr 17 '09 at 13:51
  • I ended up just creating a new column of type int for the foreign key and using that instead, never managed to get a string foreign key working. – Mr Plough Apr 18 '09 at 10:29

4 Answers4

3

Now I'm not 100% sure if this will work but have you tried a many-to-one mapping relationship?

Maybe something like this:

<many-to-one name="Players" class="DataTransfer.Player, DataTransfer"
             column="Name" property-ref="Team" />

I believe that should work, according to the NHibernate manual property-ref is an attribute that is useed for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key. This sounds like the situation you find yourself in.

Nathan Baulch
  • 20,233
  • 5
  • 52
  • 56
CalvinR
  • 744
  • 1
  • 5
  • 11
  • I gave this a try but I got the error: More than one row with the given identifier was found: Arsenal, for class: DataTransfer.Player. I should have mentioned that I'm mapping the Team -> Player relationship rather than the other way round - I've updated my original question. – Mr Plough Jan 20 '09 at 18:59
3

I think the property-ref attribute exists to solve this problem.

<bag name="Players">
   <key column="Team" property-ref="Team" />
   <one-to-many class="Player" property-ref="Team" />
</bag>
Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • It looks like NHibernate doesn't support the property-ref attribute in the one-to-many tag, and adding property-ref to just the key tag doesn't work either unfortunately. – Mr Plough Jan 20 '09 at 23:29
  • Having the property-ref in "key" is just enough. No need to have it in the many-to-many is not required. – Illuminati Jun 30 '11 at 09:31
0

I believe that you would need to use the property-ref attribute to define the field that you will be associating to. The foreign-key attribute is used to generate DDL to create the relationships (if you use that feature).

Jim Petkus
  • 4,500
  • 25
  • 19
0

Just found this: https://nhibernate.jira.com/browse/NH-1272, seems this is a bug in NHibernate, and it is fixed in 2.1.0Alpha1.

I have tried it in NHibernate 2.1.0Alpha2, and it works!

(property-ref should only be in the map tag)

Daniel Schilling
  • 4,829
  • 28
  • 60
fredrik
  • 13,282
  • 4
  • 35
  • 52