I have a table in my database called DOCUMENT_DATA which contains foreign key ID columns that point to other tables. My mapping looks like this:
Code:
<class name="Document" table="MCM.DOCUMENT_DATA" mutable="true" lazy="true">
<id column="DOCUMENT_DATAID" name="Id" unsaved-value="0">
<generator class="NHibernate.Id.SequenceGenerator">
<param name="sequence">MCM.DOCUMENT_DATA_S</param>
</generator>
</id>
<many-to-one column="DOCUMENT_TEMPLATEID" name="DocumentTemplate" cascade="none" />
<many-to-one column="MODIFIEDBY_USERMCMID" name="LastUpdatedByUser" cascade="none" />
<many-to-one column="SERVICEID" name="Service" cascade="none" />
<property column="DCN" name="Dcn"/>
</class>
My class looks like this:
Code:
public class Document
{
(... storage fields here ...)
public virtual int Id
{
get { return _id; }
set { _id = value; }
}
public virtual DocumentTemplate DocumentTemplate
{
get { return _documentTemplate; }
set { _documentTemplate = value; }
}
public virtual Service Service
{
get { return _service; }
set { _service = value; }
}
public virtual string Dcn
{
get { return _dcn; }
set { _dcn = value; }
}
public virtual User LastUpdatedByUser
{
get { return _lastUpdatedByUser; }
set { _lastUpdatedByUser = value; }
}
}
I am exposing a web service that takes in the parameters for a Document. I don't necessarily want them to have to pass a DocumentTemplate object, a Service object, and a User object across the wire because that would be a lot of information to pass, so I'm just going to make them pass ID values for those objects instead:
Code:
public void PartialSaveNewDocument(int documentTemplateId,
int serviceId, int lastUpdatedUserId, string dcn)
{
Document document = new Document();
document.DocumentTemplate = Repository.FindOne<DocumentTemplate>(documentTemplateId);
document.LastUpdatedByUser = Repository.FindOne<User>(lastUpdatedUserId);
document.Service = Repository.FindOne<Service>(serviceId);
document.Dcn = dcn;
SaveToDatabase(document);
}
Now because of how I have the NHibernate mappings set up, I have to have actual DocumentTemplate, User, and Service objects in order to save the Document, at which point NHibernate is just going to save the ID values into the database table. So I'm having to load up 3 objects from the database just to save a Document, which seems unnecessary since all the table is going to save is the IDs.
Is there some way around this (other than writing a stored procedure) so that I can just tell NHibernate to go save when all I have is the ID values? I can't change the Document object to just have ID properties for the foreign keys because (a) we need to access the information in those objects in some cases and (b) storing an ID doesn't work when another object being referenced is unsaved (the ID value will change from 0 to n when you save the object).
Jon
Code: