Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate mapping one to many not on primary key

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

like image 473
Johan Haest Avatar asked Sep 07 '12 08:09

Johan Haest


1 Answers

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>
like image 103
Stefan Flipkens Avatar answered Nov 17 '22 20:11

Stefan Flipkens