Hibernate latest version
database mysql latest version
Hi there,
I have a one to many bidirectional relationship between a single person and many books, in my code :
Session sess = factory.openSession();
Transaction y = sess.beginTransaction();
for(int i=0;i<10;i++)
{
Person p = (Person)sess.load(new Long(1));
Book b = new Book();
b.setXXX()
b.setXXX()
b.setXXX()
b.setPerson(p);
p.addToBooks(b);
sess.flush();
t.commit();
}
sess.close();
what i am trying to do is the add 10 books to the person
the books table has a person_id column to identify the person it belongs to.
Now the weird problem is that in the database is that only the last book that was added to the user , has the person_id set correctly to 1(which is correct, cos the person's id is 1), the person_id column of the rest of the first nine are all null. so what is going on? i mean i comitted after every loop, the person entry in the database is defintely there or else there would be an exception when i try to load the Person object of id=1. i have already set objects at both sides of the relationship, so is there anything else i haven done??? the hbm.xml file is defintely correct cos the person id of the last book is able to be set to 1. Can anyone help???
|