On Mar 4, 2008, at 3:39 PM, Werner Guttmann wrote:

Brett,

the sql mapping for the authors field of the class Book should read:

       <sql name="author_id"
            many-table="dw_books_authors"
            many-key="book_isbn" />



Ok, I got this working, but troubles continue. Here's my complete sql- mapping.xml:

<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" required="true">
      <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" required="true">
      <sql name="book_isbn"
           many-table="dw_books_authors"
           many-key="author_id" />
    </field>
  </class>
</mapping>

Here's 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.begin();
      Author author = new Author(1001, "Joseph", "Conrad");
      Book book = new Book("1892295490", "Heart of Darkness", author);
      database.create(book);
      // database.create(author);

      Book lookup = (Book)database.load(Book.class, "1892295490");
      System.out.println("Located book is named " + lookup.getTitle());
      System.out.println("Authors:");
for (Iterator i = lookup.getAuthors().iterator(); i.hasNext (); ) {
        Author bookAuthor = (Author)i.next();
        System.out.println("  " + bookAuthor.getFirstName() + " " +
                                  bookAuthor.getLastName());
      }
//      database.remove(lookup);
      database.commit();
      database.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

There are several things I'm seeing that don't make sense to me:

1. No entry is ever inserted into dw_books_authors. I don't understand why the mapping isn't taking care of this. When I do database.create(book), isn't that enough to trigger Castor persisting the book, the author, and the relationship between them? Additionally, I get no entries in the authors table.

2. Even if I add in the database.create(author) line -- which I don't think I should need -- this still doesn't work. In that case, an author is inserted into the authors table, but there's still nothing in dw_books_authors.

3. Despite no entries in the dw_authors or dw_books_authors, when I pull the lookup Book from the database, it DOES have a populated Author object with the right author name. I can't see how there's nothing in dw_authors or dw_book_authors, yet lookup can have a correct author entry.

Any ideas? I'm really pulling my hair out over this; I finally got the underscore thing corrected, and am running into some more trouble. I welcome any help, or any questions that I can answer to help you help me :-)

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

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


Reply via email to