Further questions. Suppose I define the mapping between books and authors both ways, like this:

<mapping>
  <class name="ibm.xml.castor.Book" identity="isbn">
    <map-to table="dw_books"/>
    <field name="isbn" type="string">
      <sql name="isbn" type="varchar" />
    </field>
    <field name="title" type="string">
      <sql name="title" type="varchar" />
    </field>
    <field name="authors" type="ibm.xml.castor.Author"
           collection="arraylist">
      <sql name="author_id"
           many-table="dw_books_authors"
           many-key="book_isbn" />
    </field>
  </class>

  <class name="ibm.xml.castor.Author" identity="id">
    <map-to table="dw_authors" />
    <field name="id" type="int">
      <sql name="id" type="integer" />
    </field>
    <field name="firstName" type="string">
      <sql name="first_name" type="varchar" />
    </field>
    <field name="lastName" type="string">
      <sql name="last_name" type="varchar" />
    </field>
    <field name="books" type="ibm.xml.castor.Book"
           collection="arraylist">
      <sql name="book_isbn"
           many-table="dw_books_authors"
           many-key="author_id" />
    </field>
  </class>
</mapping>

So now I have in the Author class a collection of Book instances. Now, let me update my test program:

public class SQLTester {
  public static void main(String args[]) {
    try {
      JDOManager.loadConfiguration("jdo-conf.xml");
      JDOManager jdoManager = JDOManager.createInstance("bmclaugh");

      Database database = jdoManager.getDatabase();
      database.setAutoStore(true);
      database.begin();
      Author author = new Author(1001, "Joseph", "Conrad");
      Author author2 = new Author(1002, "Ford Madox", "Ford");
      Book book = new Book("1892295490", "Heart of Darkness", author);
      Book book2 = new Book("0451530500", "The Secret Agent", author);
      List authorList = new LinkedList();
      authorList.add(author); authorList.add(author2);
Book book3 = new Book("0742626539", "The Inheritors", authorList); database.create(book); database.create(book2); database.create (book3);
      database.commit();

      database.begin();
      Author conrad = (Author)database.load(Author.class, 1001);
System.out.println("Located author " + conrad.getFirstName() + " "
                                           + conrad.getLastName());
      System.out.println("Books:");
      for (Iterator i = conrad.getBooks().iterator(); i.hasNext(); ) {
        Book conradBook = (Book)i.next();
        System.out.println("  " + conradBook.getTitle());
      }
      database.commit();
      database.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Here, I'm creating several books and several authors, all related. As expected, using setAutoStore() makes sure I only have to commit my books, and all the authors get committed as well. Additionally, I can verify all the entries are correctly persisted.

If I load the Author with ID 1001, I would expect the books property to get populated, based on the data within dw_books_authors; but that's not the case. The author is correctly loaded, but his books are missing (empty list). At face value, I can see that in my instances of Author at the top, I never assign anything to the books property; however, I would assume that when Castor loads the author from the database, it auto-populates the books property using the values in the dw_books_authors many-to-many table.

Right? Wrong? What am I missing, Werner? :-)

BTW, the exact output is:

Located author Joseph Conrad
Books:

Thanks
---
Brett McLaughlin
Series Editor, Head First
O'Reilly Media, Inc.

Phone: (972) 722-6252
Fax:      (972) 692-7958
E-Mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply via email to