5

This question is intended to be software / platform agnostic. I am just looking for generic SQL code.

Consider the following (very simple for example's sake) tables:

Table: Authors
id   | name
1    | Tyson
2    | Gordon
3    | Tony
etc

Table: Books
id   | author   | title
1    | 1        | Tyson's First Book
2    | 2        | Gordon's Book
3    | 1        | Tyson's Second Book
4    | 3        | Tony's Book
etc

Table: Stores
id   | name
1    | Books Overflow
2    | Books Exchange
etc

Table: Stores_Books
id   | store   | book
1    | 1       | 1
2    | 2       | 4
3    | 1       | 3
4    | 2       | 2

As you can see, there is a one-to-many relationship between Books and Authors, and a many-to-many relationship between Books and Stores.

Question one: What is the best query to eager load one author and their books (and where the books are sold) into an object-oriented program where each row is representative of an object instance?

Question two: What is the best query to eager load the entire object tree into an object-oriented program where each row is representative of an object instance?

Both of these situations are easy to imagine with lazy loading. In either situation you would fetch the author with one query and then as soon as you need their books (and what stores the books are sold at) you would use another query to get that information.

Is lazy loading the best way to do this or should I use a join and parse the result when creating the object tree (in an attempt to eager load the data)? In this situation what would be the optimal join / target output from the database in order to make parsing as simple as possible?

As far as I can tell, with eager loading, I would need to manage a dictionary or index of some sort of all the objects while I am parsing the data. Is this actually the case or is there a better way?

3 Answers 3

3

That's a tough question to answer. I've done this before by writing a query that returns everything as a flat table and then looping through the results, creating objects or structures as the most-significant columns change. I think that works better than multiple database calls because there's a lot of overhead involved in each call, though depending on how many smaller entities there are to each big entity that might not be best.

The following might apply to both your questions 1 and 2.

SELECT a.id, a.name, b.id, b.name FROM authors a LEFT JOIN books b ON a.id=b.author

(pseudocode, in your program that makes the db call)

while (%row=fetchrow) {
   if ($row{a.id} != currentauthor.id) {
      currentauthor.id=$row{a.id};
      currentauthor.name=$row{a.name};
      }
    currentbook=new book($row{b.id, b.name});
    push currentauthor.booklist, currentbook;
    }

[edit] I just realized I didn't answer the second part of your question. Depending on the size of the data for stores and what I intended doing with it, I would either

Before looping through books/authors as above, slurp the whole stores table into a structure in my program, much like the book/author structure above but indexed by the storeid, and then do a lookup in that structure every time I read a book record and store a reference to the store table

or, if there are many stores,

Join the stores onto the books and have an additional nested loop to add stores objects within the part of the code that adds a book.

Here's a relevant Wikipedia article: http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch

I hope that helps!

4
  • Your pseudo code gave me an idea bout local variable scope. As long as I tell the database to sort its output based on a known criteria (such as author and then book, as in your example) then I don't have to keep a local index / dictionary of objects because I can just reference "currentAuthor".
    – knpwrs
    Jul 15, 2011 at 5:47
  • Yes, that's it exactly. You probably don't even need to tell the database to sort the columns by a.id because it will return them in that order unless you tell it to sort some other way. (I'm not sure databases guarantee that behavior, but that's what they do.)
    – ratsbane
    Jul 15, 2011 at 5:52
  • 1
    Here's a followup question. What about if the Books and Authors were also many-to-many (in addition to the Books and Stores)? I would then need an application-side index / dictionary, right?
    – knpwrs
    Jul 15, 2011 at 5:57
  • Yes, that's right - at least, as long as you want your in-memory structure to be normalized. Depending on how much data you have, how much coding you want to do, etc., you could just create a new bookstore object each time you encounter a bookstore for each book, though this would lead to much duplication. Also, in my answer I suggested slurping the whole stores table into a structure at start. A better solution might be to build that structure as you add book objects. Just have one big query that returns a separate row for each author - book - store, look up store and add if needed.
    – ratsbane
    Jul 15, 2011 at 6:10
2

In an OO program you don't use SQL, instead you let that be done invisibly by your Persistence mechanism. To explain:

If you have an object-oriented program then you want an object model that natuarally represents the concepts of Author, Book and Store. You then have an "Object/Relational mapping" problem. Somehow you want to get data from the database using SQL and yet work naturally with your objects.

In the Java world we do that with the Java Persistence API (JPA). You don't actually write the SQL instead you just "annotate" the Java Class to say "This class corresponds to that Table, this attribute to that column", and then do some interesting things with the JOINs and can in fact choose either Lazy or Eager loading as it makes sense.

So you might end up with an Author class (I'm making attributes public here for brevity, in real life we have private attributes and getters and setters.

 @Entity
 public Class Author {
     public int id; 
     public String name;
     // more in a minute

That class is annotated as an entity and so JPA with match up the atrributes in the objects with their columns in the corresponding table. The annotations have more capabilities so that you can specify mappings between names of attributes and columns that don't exactly match; mappings such as

    PUBLISHED_AUTHOR => Author,  
    FULL_NAME => name

Now what about JOINS and relationships? The author class has a collection of Books

   @Entity
   public Class Author {
     public int id; 
     public String name;
     public List<Book> books;

and the Book class has an attribute that is it's author

   @Entity
   public Class Book {
       public int id;
       public String title
       public Author author

The JPA Entity Manager class fetches an instance of Book using a find method (I'll not go into detail here)

   int primaryKey = 1;
   Book aBook = em.find( primaryKey); // approximately

Now your code can just go

   aBook.author.name  

You never see the fact that SQL was used to fetch the data for Book, and by the time you ask for the author attribute has also fetched the author data. A SQL JOIN may well have been used, you don't need to know. You can control whether the fetch is Eager or Lazy by more annotations.

Similarly

    int primaryKey = 2
    Author author = em.find( primaryKey ); 

    author.books.size() ; // how many books did the author write?

we get a list of all the books as well as the authors other data, SQL happened, we didn't see it.

3
  • I am aware of object relational mappers such as Hibernate/JPA, but I was more interested in the best way to do eager loading from scratch.
    – knpwrs
    Jul 15, 2011 at 5:45
  • Nice introduction! But which annotation controls "whether the fetch is Eager or Lazy"? How to use it and where to specify? How can I get list of all authors from the database to display them let's say in a JTree? How then simply get list of all stores where books from a certain author is sold? I see too many questions ahead.
    – Dime
    Jul 15, 2011 at 9:01
  • @Dime, thanks. There is a whole lot more to JPA including a query language to deal with your stores issue. I don't see much point in trying to reproduce the good tutorial material out there - I just wanted to give a flavour of why OO folks don't need SQL. But to control Lazy/Eager it's just an extension of the @OneToMany annotation @OneToMany(fetch=FetchType.EAGER) private Collection myCollection;
    – djna
    Jul 15, 2011 at 9:57
1

Here is some T-SQL to get you started:

1.

select a.name, b.title from Authors a join Books b on a.id = b.author

2.

select a.name, b.title, s.name from Authors a join Books b on a.id = b.author join Stores_Books sb on sb.book = b.id join Stores s on s.id = sb.store

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.