I'm trying to start learning hibernate with a simple example, but have managed to get myself tangled up in my own underwear already.
I wanted to map books and authors as a many to many relationship. The authors have order, so each Book has a List of Authors. An author has a Set of Books. I create some books, then open a hibernate session and call session.saveOrUpdate(book); on them.
What happens is my book table is populated nicely, as is the author table, but book_authors (the association table) is left totally empty.
Any clues to a confuzzled beginner would be greatly appreciated. Thanks!
create table book_authors (author_id bigint not null, book_id bigint not null, author_order integer not null, primary key (book_id, author_order))
create table Author (id bigint not null auto_increment, firstName varchar(255), lastName varchar(255), primary key (id))
create table Book (id bigint not null auto_increment, title varchar(255) not null, isbn varchar(255), primary key (id))
alter table book_authors add index FKCBA3F8F23DAE231 (book_id), add constraint FKCBA3F8F23DAE231 foreign key (book_id) references Book (id)
alter table book_authors add index FKCBA3F8F257F3E04F (author_id), add constraint FKCBA3F8F257F3E04F foreign key (author_id) references Author (id)
...
21:38:03,812 INFO Configuration:350 - Mapping resource: cbare/books/Book.hbm.xml
21:38:04,796 DEBUG DTDEntityResolver:20 - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath under net/sf/hibernate/
21:38:04,812 DEBUG DTDEntityResolver:29 - found
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath
21:38:05,171 INFO Binder:229 - Mapping class: cbare.books.Book -> Book
21:38:05,343 DEBUG Binder:486 - Mapped property: id -> id, type: long
21:38:05,375 DEBUG Binder:486 - Mapped property: title -> title, type: string
21:38:05,375 DEBUG Binder:486 - Mapped property: isbn -> isbn, type: string
21:38:05,375 INFO Binder:571 - Mapping collection: cbare.books.Book.authors -> book_authors
21:38:05,406 DEBUG Binder:486 - Mapped property: authors, type: java.util.List
21:38:05,406 INFO Configuration:350 - Mapping resource: cbare/books/Author.hbm.xml
21:38:05,421 DEBUG DTDEntityResolver:20 - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath under net/sf/hibernate/
21:38:05,421 DEBUG DTDEntityResolver:29 - found
http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath
21:38:05,484 INFO Binder:229 - Mapping class: cbare.books.Author -> Author
21:38:05,484 DEBUG Binder:486 - Mapped property: id -> id, type: long
21:38:05,484 DEBUG Binder:486 - Mapped property: firstName -> firstName, type: string
21:38:05,484 DEBUG Binder:486 - Mapped property: lastName -> lastName, type: string
21:38:05,484 INFO Binder:571 - Mapping collection: cbare.books.Author.books -> book_authors
21:38:05,500 DEBUG Binder:486 - Mapped property: books, type: java.util.Set
21:38:05,500 INFO Configuration:627 - processing one-to-many association mappings
21:38:05,500 DEBUG Binder:1353 - Second pass for collection: cbare.books.Book.authors
21:38:05,515 DEBUG Binder:1368 - Mapped collection key: book_id, index: author_order, element: author_id, type: cbare.books.Author
21:38:05,515 DEBUG Binder:1353 - Second pass for collection: cbare.books.Author.books
21:38:05,515 DEBUG Binder:1368 - Mapped collection key: author_id, element: book_id, type: cbare.books.Book
21:38:05,515 INFO Configuration:636 - processing one-to-one association property references
21:38:05,515 INFO Configuration:661 - processing foreign key constraints
21:38:05,515 DEBUG Configuration:678 - resolving reference to class: cbare.books.Book
21:38:05,515 DEBUG Configuration:678 - resolving reference to class: cbare.books.Author
...
21:38:07,453 DEBUG SessionImpl:555 - opened session
21:38:07,453 DEBUG SessionImpl:1387 - saveOrUpdate() unsaved instance
21:38:07,453 DEBUG SessionImpl:825 - saving [cbare.books.Book#<null>]
21:38:07,468 DEBUG SessionImpl:2309 - executing insertions
21:38:07,468 DEBUG Cascades:497 - processing cascades for: cbare.books.Book
21:38:07,484 DEBUG Cascades:506 - done processing cascades for: cbare.books.Book
21:38:07,500 DEBUG WrapVisitor:81 - Wrapped collection in role: cbare.books.Book.authors
21:38:07,515 DEBUG EntityPersister:490 - Inserting entity: cbare.books.Book (native id)
21:38:07,515 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets
21:38:07,515 DEBUG DriverManagerConnectionProvider:84 - total checked-out connections: 0
21:38:07,515 DEBUG DriverManagerConnectionProvider:90 - using pooled JDBC connection, pool size: 0
21:38:07,515 DEBUG SQL:226 - insert into Book (title, isbn) values (?, ?)
Hibernate: insert into Book (title, isbn) values (?, ?)
21:38:07,531 DEBUG BatcherImpl:249 - preparing statement
21:38:07,562 DEBUG EntityPersister:388 - Dehydrating entity: [cbare.books.Book#<null>]
21:38:07,578 DEBUG AbstractEntityPersister:1236 - Natively generated identity: 1
21:38:07,578 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets
21:38:07,578 DEBUG BatcherImpl:269 - closing statement
21:38:07,593 DEBUG Cascades:497 - processing cascades for: cbare.books.Book
21:38:07,593 DEBUG Cascades:524 - cascading to collection: cbare.books.Book.authors
21:38:07,593 DEBUG Cascades:113 - cascading to saveOrUpdate()
21:38:07,609 DEBUG SessionImpl:1387 - saveOrUpdate() unsaved instance
21:38:07,609 DEBUG SessionImpl:825 - saving [cbare.books.Author#<null>]
21:38:07,609 DEBUG SessionImpl:2309 - executing insertions
21:38:07,609 DEBUG EntityPersister:490 - Inserting entity: cbare.books.Author (native id)
21:38:07,609 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets
21:38:07,625 DEBUG SQL:226 - insert into Author (firstName, lastName) values (?, ?)
Hibernate: insert into Author (firstName, lastName) values (?, ?)
21:38:07,625 DEBUG BatcherImpl:249 - preparing statement
21:38:07,625 DEBUG EntityPersister:388 - Dehydrating entity: [cbare.books.Author#<null>]
21:38:07,625 DEBUG AbstractEntityPersister:1236 - Natively generated identity: 1
21:38:07,625 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets
21:38:07,625 DEBUG BatcherImpl:269 - closing statement
21:38:07,640 DEBUG Cascades:113 - cascading to saveOrUpdate()
21:38:07,640 DEBUG SessionImpl:1387 - saveOrUpdate() unsaved instance
21:38:07,640 DEBUG SessionImpl:825 - saving [cbare.books.Author#<null>]
21:38:07,640 DEBUG SessionImpl:2309 - executing insertions
21:38:07,640 DEBUG EntityPersister:490 - Inserting entity: cbare.books.Author (native id)
21:38:07,640 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets
21:38:07,640 DEBUG SQL:226 - insert into Author (firstName, lastName) values (?, ?)
Hibernate: insert into Author (firstName, lastName) values (?, ?)
21:38:07,640 DEBUG BatcherImpl:249 - preparing statement
21:38:07,640 DEBUG EntityPersister:388 - Dehydrating entity: [cbare.books.Author#<null>]
21:38:07,656 DEBUG AbstractEntityPersister:1236 - Natively generated identity: 2
21:38:07,656 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets
21:38:07,656 DEBUG BatcherImpl:269 - closing statement
21:38:07,656 DEBUG Cascades:506 - done processing cascades for: cbare.books.Book
21:38:07,656 DEBUG SessionImpl:1387 - saveOrUpdate() unsaved instance
21:38:07,656 DEBUG SessionImpl:825 - saving [cbare.books.Book#<null>]
21:38:07,656 DEBUG SessionImpl:2309 - executing insertions
21:38:07,656 DEBUG Cascades:497 - processing cascades for: cbare.books.Book
21:38:07,656 DEBUG Cascades:506 - done processing cascades for: cbare.books.Book
21:38:07,656 DEBUG WrapVisitor:81 - Wrapped collection in role: cbare.books.Book.authors
21:38:07,656 DEBUG EntityPersister:490 - Inserting entity: cbare.books.Book (native id)
21:38:07,656 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets
21:38:07,656 DEBUG SQL:226 - insert into Book (title, isbn) values (?, ?)
Hibernate: insert into Book (title, isbn) values (?, ?)
...