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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With