Note that there are some explanatory texts on larger screens.

plurals
  1. POnhibernate - sproutcore : How to only retrieve reference ID's and not load the reference/relation?
    primarykey
    data
    text
    <p>I use as a front-end sproutcore, and as back-end an nhibernate driven openrasta REST solution.</p> <p>In sproutcore, references are actualy ID's / guid's. So an Address entity in the Sproutcore model could be:</p> <p><code>// sproutcore code</code></p> <p><code>App.Address = App.Base.extend(</code></p> <p><code>street: SC.Record.attr(String, { defaultValue: "" }),</code></p> <p><code>houseNumber: SC.Record.attr(String),</code></p> <p><code>city: SC.Record.toOne('Funda.City') </code></p> <p><code>);</code> </p> <p>with test data:</p> <p><code>Funda.Address.FIXTURES = [<code></p> <p><code> { guid: "1",</code></p> <p><code>street: "MyHomeStreet",</code></p> <p><code>houseNumber: "34",</code></p> <p><code>city: "6" </code> <code> } </code></p> <p><code>]</code></p> <p>Here you see that the reference city has a value of 6. When, at some point in your program, you want to use that reference, it is done by: myAddress.Get("city").MyCityName</p> <p>So, Sproutcore automatically uses the supplied ID in a REST Get, and retrieves the needed record. If the record is available in de local memory of the client (previously loaded), then no round trip is made to the server, otherwise a http get is done for that ID : "http://servername/city/6". Very nice.</p> <p>Nhibernate (mapped using fluent-nhibernate):</p> <p><code> public AddressMap() { Schema(Config.ConfigElement("nh_default_schema", "Funda"));</p> <pre><code> Not.LazyLoad(); //Cache.ReadWrite(); Id(x =&gt; x.guid).Unique().GeneratedBy.Identity(); Table("Address"); Map(x =&gt; x.street); Map(x =&gt; x.houseNumber); References(x =&gt; x.city, </code></pre> <p>"cityID").LazyLoad().ForeignKey("fk_Address_cityID_City_guid");<br> } </code></p> <p>Here i specified the foreign key, and to map "cityID" on the database table. It works ok. BUT (and these are my questions for the guru's):</p> <ol> <li>You can specify to lazy load / eager load a reference (city). Off course you do not want to eager load all your references. SO generally your tied to lazy loading. But when Openrast (or WCF or ...) serializes such an object, it iterates the properties, which causes all the get's of the properties to be fired, which causes all of the references to be lazy loaded. </li> </ol> <p>SO if your entity has 5 references, 1 query for the base object, and 5 for the references will be done. You might better be off with eager loading then .... This sucks... Or am i wrong?</p> <ol> <li>As i showed how the model inside sproutcore works, i only want the ID's of the references. So i Don't want eagerloading, and also not lazy loading. just a "Get * from Address where ID = %" and get that mapped to my Address entity. THen i also have the ID's of the references which pleases Sproutcore and me (no loading of unneeded references). But.... can NHibernate map the ID's of the references only? And can i later indicate nHibernate to fully load the reference?</li> </ol> <p>One approach could be (but is not a nice one) to load all reference EAGER (with join) (what a waste of resources.. i know) and in my Sever-side Address entity:</p> <p><code></p> <pre><code> // Note: NOT mapped as Datamember, is NOT serialized! public virtual City city { get; set; } Int32 _cityID; [Datamember] public virtual Int32 cityID { get { if (city != null) return city .guid; else return _cityID; } set { if (city!= null &amp;&amp; city.guid != value) { city= null; _cityID = value; } else if (city == null) { _cityID = value; } } } </code></pre> <p></code></p> <p>So i get my ID property for Sproutcore, but on the downside all references are loaded. A better idea for me???</p> <ol> <li>nHibernate-to-linq</li> </ol> <p>3a. I want to get my address without their references (but preferably with their id's)</p> <p> Dao myDao = new Dao(); from p in myDao.All() select p; </code></p> <p>If cities are lazy loading in my mapping, how can i specify in the linq query that i want it also to include my city id only? </p> <p>3b.</p> <p>I want to get addresses with my cities loaded in 1 query: (which are mapped as lazyloaded)</p> <p> Dao myDao = new Dao(); from p in myDao.All() join p.city ??????? select p; </code></p> <ol> <li>My Main Question: As argued earlier, with lazy loading, all references are lazy loaded when serializing entities. How can I prevent this, and only get ID's of references in a more efficient way?</li> </ol> <p>Thank you very much for reading, and hopefully you can help me and others with the same questions. Kind regards.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload