NHibernate mapping one to many not on primary key
Asked Answered
S

1

9

I'm working with a legacy database, and I have the following tables:

Person                  ExternalFile
------                  ------------
Id           (int, PK)  Key           (string)
ConnectionId (int)      Type          (int)
Name         (string)   ConnectionId  (int)
Firstname    (string)   Path          (string)
                        Id            (int, PK)

A personal has many external files.

Example given:

Person A                ExternalFile1          ExternalFile2
--------                -------------          -------------
Id: 1                   Key: 'WN'              Key: 'WN'
ConnectionId: 29        Type: 4                Type: 4
Name: 'Foo'             ConnectionId: 29       ConnectionId: 29
Firstname: 'Bar'        Path: 'C:/file1.txt'   Path: 'D:/file2.txt'
                        Id: 1                  Id: 2

Can I map this so that a user has a bag of ExternalFiles?

My mappingfiles

ExternalFile:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Not.Relative" assembly="Not">
  <class name="ExternalFile" table="tbl_externalfiles" lazy="false">
    <id name="Id" column="`Id`">
      <generator class="identity" />
    </id>
    <property name="Key" column="`CDKey`" />
    <property name="ConnectionId" column="`KeyValue`" />
    <property name="Type" column="`DocType`"  />
    <property name="Path" column="`Path`" />
  </class>
</hibernate-mapping>

Person:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Not.Relative" assembly="Not">
  <class name="Person" table="tbl_person" lazy="false">
    <id name="Id" column="`Id`">
      <generator class="identity" />
    </id>
    <property name="ConnectionId" column="`pairid`" />
    <property name="Name" column="`name`" />
    <property name="Firstname" column="`firstname`"  />
  </class>
</hibernate-mapping>

My query would look like this:

SELECT * 
FROM tbl_externalfiles
WHERE KeyValue = @p0
AND   CDKey = @p1
AND   DocType = @p2

@p0 = 29, @p1 = 'WN', @p2 = 4

So I have to give 3 parameters to the bag from persons to External files,

1: the ConnectionID of the person

2: 'WN' <-- always the same (the key)

3: 4 <-- always the same (the type)

On a many to one this works when I map to a composite ID, but I can't use that in this case.

Is a bag even possible here? Should I just use an extra query to fetch my ExternalFiles where I use a criteria to query.

EDIT:

I know I can do the following for only the connectionId

<bag name="Files" lazy="false">
  <key column="KeyValue" property-ref="ConnectionId"/>
  <one-to-many class="ExternalFile" />
</bag>

But I don't know how to get the Type and Key parameters in there aswell.

Thanks

Subdominant answered 7/9, 2012 at 8:9 Comment(0)
P
13

Try this

   <bag name="Files" lazy="false" where="DocType = '4' AND CDKey = 'WN'">
      <key column="KeyValue" property-ref="ConnectionId" />
      <one-to-many class="ExternalFile" />
    </bag>
Pirozzo answered 7/9, 2012 at 10:4 Comment(1)
Awesome! I did not think about this even.Dendroid

© 2022 - 2024 — McMap. All rights reserved.