15.01.2013 Views

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 - Object Relational Mappers<br />

supports OO principles like inheritance and polymorphism. Either query methods are abstractions on<br />

top <strong>of</strong> SQL, which means you get total portability – all you need to do to target a different database is<br />

change your dialect configuration.<br />

HQL works <strong>of</strong>f <strong>of</strong> the IQuery interface, which is created by calling CreateQuery on your session. With<br />

IQuery you can return individual entities, collections, substitute parameters and more. Here are some<br />

examples:<br />

string lastName = "allen";<br />

ISession session = _sessionFactory.OpenSession();<br />

//retrieve a salesperson by last name<br />

IQuery query = s.CreateQuery("from SalesPerson p where p.LastName =<br />

'allen'");<br />

SalesPerson p = query.UniqueResult();<br />

//same as above but in 1 line, and with the last name as a variable<br />

SalesPerson p = session.CreateQuery("from SalesPerson p where p.LastName =<br />

?").SetString(0, lastName).UniqueResult();<br />

//people with few sales<br />

IList slackers = session.CreateQuery("from SalesPerson person<br />

where size(person.Sales) < 5").List();<br />

This is just a subset <strong>of</strong> what can be accomplished with HQL (the downloadable sample has slightly more<br />

complicated examples).<br />

Lazy Loading<br />

When we load a salesperson, say by doing: SalesPerson person =<br />

session.Get(1); the Sales collection won’t be loaded. That’s because, by default,<br />

collections are lazily loaded. That is, we won’t hit the database until the information is specifically<br />

requested (i.e., we access the Sales property). We can override the behavior by setting<br />

lazy=”false” on the bag element.<br />

The other, more interesting, lazy load strategy implemented by NHibernate is on entities themselves.<br />

You’ll <strong>of</strong>ten want to add a reference to an object without having to load the actual object from the<br />

database. For example, when we add a Sales to a SalesPerson, we need to specify the Model, but<br />

don’t want to load every property – all we really want to do is get the Id so we can store it in the<br />

ModelId column <strong>of</strong> the Sales table. When you use session.Load(id) NHibernate will load a<br />

proxy <strong>of</strong> the actual object (unless you specify lazy=”false” in the class element). As far as you’re<br />

concerned, the proxy behaves exactly like the actual object, but none <strong>of</strong> the data will be retrieved from<br />

the database until the first time you ask for it. This makes it possible to write the following code:<br />

Sale sale = new Sale(session.Load(1), DateTime.Now, 46000.00);<br />

salesPerson.AddSales(sale);<br />

<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />

51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!