-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Optimizing saves when saving foreign keys
PostPosted: Fri Feb 23, 2007 11:08 am 
Newbie

Joined: Thu May 04, 2006 2:42 pm
Posts: 10
Location: Columbus, OH
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:

_________________
Jon Kruger
http://jonkruger.com/blog


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 11:13 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
ISession.Load was designed just for this purpose. Use ISession.Load to load the DocumentTemplate, User, and Service. It will in fact return you proxies for those objects without going to the database, and you can use the proxies instead of the real objects.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 11:29 am 
Newbie

Joined: Thu May 04, 2006 2:42 pm
Posts: 10
Location: Columbus, OH
So ISession.Load() will essentially load up an object that looks like a real object, but it won't load the actual data unless I try and access a property of the object that requires the data to be there?

_________________
Jon Kruger
http://jonkruger.com/blog


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 11:37 am 
Newbie

Joined: Thu May 04, 2006 2:42 pm
Posts: 10
Location: Columbus, OH
Also, do my objects have to have lazy loading turned on for this to work?

_________________
Jon Kruger
http://jonkruger.com/blog


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 11:42 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, and yes.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.