Hi Brett,
your mapping of the n:m relation looks okay to me. I use similar
relations in my applications without problems. The only difference that
may have an impact is, that you set required="true" on the mapping of
the relation. Have you ever tried to remove that attribute?
What do you like to achive by setting required="true" on that relation?
Regards
Ralf
Brett McLaughlin schrieb:
> I'm having trouble getting a fairly simple M:N relationship working.
> Here's the basic setup.
>
> Table: authors
> id: integer
> first_name: varchar
> last_name: varchar
>
> Table: books
> isbn: varchar
> title: varchar
>
> Table: books_authors
> book_isbn: varchar
> author_id: integer
>
> So this is a relationship that goes both ways; A single book can have
> multiple authors, and an author can relate to several books.
>
> My classes are as you'd expect; Book has get/setTitle(), get/setIsbn(),
> and get/setAuthors(). Author has get/setId(), get/setFirstName(),
> get/setLastName(), and get/setBooks(). Both use Lists for the
> collections of authors/books.
>
> Here's my mapping file, where I believe the problem lies:
>
> <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>
>
> If I create a new Book/Author combination, like this:
>
> 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);
>
> --then I get an error like this:
>
> A fatal error occurred while creating/updating ibm.xml.castor.Book using
> SQL: INSERT INTO dw_books (isbn,title,author_id) VALUES (?,?,?)
>
> For reasons I can't figure out (I've been working on this for about
> three hours now), Castor thinks that the field defined as a many-to-many
> field -- author_id in the books_authors table -- is actually in the
> books table. I've worked through the JDO examples from Castor, as well
> as the online docs, and I can't find a single thing I'm doing wrong,
> which of course means it's something quite obvious.
>
> Any help would be really great. BTW, here's the more complete test
> program, in case there's something about how you create an entry in an
> M:N situation that's different from how you create one normally...
>
> 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.commit();
> database.close();
>
> Thanks; I was able to create an Author or Book in the database when I
> removed the M:N stuff, but of course then my data's all out of whack.
>
> Thanks
> ---
> Brett McLaughlin
> Series Editor, Head First
> O'Reilly Media, Inc.
>
> Phone: (972) 722-6252
> Fax: (972) 692-7958
> E-Mail: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
>
>
--
Syscon Ingenieurbüro für Meß- und Datentechnik GmbH
Ralf Joachim
Raiffeisenstraße 11
72127 Kusterdingen
Germany
Tel. +49 7071 3690 52
Mobil: +49 173 9630135
Fax +49 7071 3690 98
Internet: www.syscon.eu
E-Mail: [EMAIL PROTECTED]
Sitz der Gesellschaft: D-72127 Kusterdingen
Registereintrag: Amtsgericht Stuttgart, HRB 382295
Geschäftsleitung: Jens Joachim, Ralf Joachim
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email