I tried just about every combinatoric possibility of inverse="true" and inverse="false" (which should be redundant as it is the default). No luck.
I actually don't think the problem is with the mapping files (although I'm a clueless newbie and could be wrong) because the mapping files are so similar to the examples. Also, SchemaExport correctly creates the tables, so they can't be that far off.
I think the problem might have something to do with how I'm using my POJOs. I think something is causing hibernate's interceptors to fail. For example check this code snippet out:
Code:
Session session = null;
try {
session = factory.openSession();
Author ha = new Author("Harold", "Abelson");
Author gjs = new Author("Gerald Jay", "Sussman");
Book sicp = new Book(
"Structure and Interpretation of Computer Programs - 2nd Ed.",
"0262011530");
sicp.addAuthor(ha);
sicp.addAuthor(gjs);
session.saveOrUpdate(sicp);
}
finally {
if (session!=null) session.close();
}
This results in the book being written to the db, both authors being written and nothing in the join table, via the following SQL:
Code:
Hibernate: insert into books (title, isbn) values (?, ?)
Hibernate: insert into authors (firstName, lastName) values (?, ?)
Hibernate: insert into authors (firstName, lastName) values (?, ?)
Now, it gets more interesting when I try this:
Code:
Session session = null;
try {
session = factory.openSession();
Author ha = new Author("Harold", "Abelson");
Author gjs = new Author("Gerald Jay", "Sussman");
Book sicp = new Book(
"Structure and Interpretation of Computer Programs - 2nd Ed.",
"0262011530");
session.saveOrUpdate(sicp);
System.out.println("after first save!");
sicp.addAuthor(ha);
sicp.addAuthor(gjs);
session.saveOrUpdate(sicp);
}
finally {
if (session!=null) session.close();
}
or even this:
Code:
Session session = null;
try {
session = factory.openSession();
Author ha = new Author("Harold", "Abelson");
Author gjs = new Author("Gerald Jay", "Sussman");
Book sicp = new Book(
"Structure and Interpretation of Computer Programs - 2nd Ed.",
"0262011530");
session.saveOrUpdate(sicp);
System.out.println("after first save!");
sicp.addAuthor(ha);
sicp.addAuthor(gjs);
sicp.setTitle("Structure and Interpretation of Computer Programs - 2nd Edition");
session.saveOrUpdate(sicp);
}
finally {
if (session!=null) session.close();
}
Both result in the following output, and situation in the DB:
Code:
Hibernate: insert into books (title, isbn) values (?, ?)
after first save!
Code:
mysql> select * from books; select * from authors; select * from book_author;
+---------+-------------------------------------------------------------+------------+
| book_id | title | isbn |
+---------+-------------------------------------------------------------+------------+
| 1 | Structure and Interpretation of Computer Programs - 2nd Ed. | 0262011530 |
+---------+-------------------------------------------------------------+------------+
1 row in set (0.00 sec)
Empty set (0.00 sec)
Empty set (0.00 sec)
Note that there is just one write to the DB. Even the update to the title field is not persisted. So, maybe this is a clue about the root of the problem. It looks to me like the interceptors that do dirty checking are somehow not in place or are not working. And maybe those interceptors are also involved in persisting collection information.
I'm guessing that hibernate is supposed to do some byte-code mojo the first time a persistent object is written to the DB, but somehow its failing to do so in my situation.
This is driving me nutty! My kindom for a clue! Sadly my kingdom does not amount to very much, and I was hopelessly nutty to begin with, but still...
Thanks for the replies.
-chris