-->
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: Wasted database calls with conditional selections
PostPosted: Wed May 25, 2005 3:03 pm 
Newbie

Joined: Wed May 11, 2005 6:14 pm
Posts: 10
Okay, this is an issue I ran into the other day, which, coincedentally, somebody else did on the http://asp.net forums. They articulated the problem perfectly, so I'll post their post here:

Quote:
The 2nd thing that I found a bit constricting is when you have a database table that looks like this:

car_id, bigint
car_name, varchar(100)
manufacturer_id, bigint --foreign key reference to another table


and a matching class that looks like this:

public class Car
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; } //you'll see why I have a setter in a minute
}

private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}


private Manufacturer _mfg;
public Manufacturer Manufacturer
{
get { return _mfg; }
set { _mfg = value; }
}

//more properties and methods below
}

Okay, simple enough. Now the difference you see here is that the database has a single integer value for the reference to the other table (manufacturers). The class has an actual object, not just a manufacturer_id. Now picture we are on a form creating a new car, and we have a dropdown list of manufacturers (which, btw, in my NHibernate framework is as easy as:

dropdown.DataSource = Manufacturer.GetAll();

). Okay, so the user types in a name and selects a manufacturer from the list, and then clicks a button to save it:

private void btnSaveCar_Click(object sender, EventArgs e)
{
Car car = new Car();
car.Name = txtName.Text;
car.Manufacturer = //what do we put here? we have 2 choices

//we can get the entire object from the database (wast of a call)
car.Manufacturer = Manufacturer.Get( ddlManufacturers.SelectedValue );

//or we can create a dummy object
Manufacturer mfg = new Manufacturer();
mfg.ID = ddlManufacturers.SelectedValue; //fool our object by just providing the id

//finally, save the object
car.Save();
}

Now, the 2nd method I don't like at all, for 1 because there is no reason to ever SET the ID field, and 2, we are dealing with a fake object. If we make further changes to the car the updates could ripple down to the child objects (and may cause undesired results). So I have stuck with method 1, using Get and wasting a call to the database, with the advantage that I am working with a true object.

Anyway, I have yet to do any really complex querying, and that's really the part I am dreading, but other than that I have saved tons of time by not writing any SQL at all.


I also resorted to method 1 (the wasted database call) because it was the "cleanest". How are others handling this common scenario? Dummy objects just feels... wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 3:10 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You can either use ISession.Load instead of ISession.Get, this will give you a proxy and won't hit the database, or you can enable the second level cache, then the object will be cached when first loaded and you won't hit the database next time.


Top
 Profile  
 
 Post subject: Ahh
PostPosted: Wed May 25, 2005 3:18 pm 
Newbie

Joined: Wed May 11, 2005 6:14 pm
Posts: 10
sergey wrote:
You can either use ISession.Load instead of ISession.Get, this will give you a proxy and won't hit the database, or you can enable the second level cache, then the object will be cached when first loaded and you won't hit the database next time.


Ah ha! So, for instance, when binding the list, use the Load method rather than Get for the collection? All items in this collection will be cached?

If so, I guess it would be nice to have a separate abstracted method to return a cached list of objects for this purpose. Neat.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 3:21 pm 
Newbie

Joined: Wed May 11, 2005 6:14 pm
Posts: 10
I had another thought, though. Let's say the dropdown list contains a large number (> 200) of options, for whatever reason (I guess you could argue there needs to be some filter there anyway, but let's get academic).

In a dropdown list, you're only going to display (usually) like a description/name field, and some identifier in the value field. By binding the list to a collection of these 200 objects, you're loading and caching quite a bit of data just to be able to pass the ID around. Imagine a large number of users loading this page, and suddenly a boatload of unnecessary stuff is cached.

How can you handle this, if the cache only applies to the current page's session instance?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 5:42 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
The second-level cache is independent of the session (it belongs to the session factory). The problem you describe though would probably be better solved by the query cache, but it isn't in NHibernate yet.


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.