-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate bug???
PostPosted: Wed Jun 30, 2004 9:46 pm 
Beginner
Beginner

Joined: Sun Jun 20, 2004 11:39 pm
Posts: 24
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???


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 11:08 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Are you adding the 'Book's to a Set on the Person? If so, what does your hashcode and equals methods look like on your Book class? It is possible that when you add a book you are actually replacing it in your Set.

Have you tried turning on the show SQL option and looking to see if Hibernate is executing an Update or an Insert? What is your primary key on the Book object?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 11:27 pm 
Beginner
Beginner

Joined: Sun Jun 20, 2004 11:39 pm
Posts: 24
yes, i am adding a book to a set on the Person.

this is the sql that is shown when the show_sql option is set to true:

Hibernate: insert into Book (name, price, id) values (?, ?, ?)
Hibernate: update Book set person_id=null where person_id=? and id=?
Hibernate: update Book set person_id=? where id=?

The primary key of the Book object is id.

This is how the hash and equals method look like in Book

public boolean equals (Object obj) {
if (null == obj) return false;
if (!(obj instanceof Book)) return false;
else {
Book mObj = (Book) obj;
return (this.getId() == mObj.getId());
}
}


public int hashCode () {
if (Integer.MIN_VALUE == this.hashCode) {
return (int) this.getId();
}
return this.hashCode;
}


"It is possible that when you add a book you are actually replacing it in your Set. "

Regarding the above sentence, i dont think i am actually replacing the previous book added in the set cos i can see the entry in the Book table for each loop. but the problem is that the person id of the past nine entries are all null except the last one. i mean i do a save and saveOrUpdate on the book and person object respectively so they both should get saved .

This is driving me nuts!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 11:30 pm 
Beginner
Beginner

Joined: Sun Jun 20, 2004 11:39 pm
Posts: 24
Moreover, i create a new Book each time for each iteration of the loop so it represents a new entry and i set the user and book on both sides of the relationship before i commit but the user_id still doesnt gets saved in the Book table except the last one. Seriously, i thought that hibernate is supposed to make my life easier but it is making my life harder and more complicated then normal sql statements. I really hope that someone can help me out with this one


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 11:41 pm 
Beginner
Beginner

Joined: Sun Jun 20, 2004 11:39 pm
Posts: 24
Problem solved!!!

Correction to the code in the first posting, the below is the code that is currently giving problems, forgot to add in the code in red:

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.save(b);
sess.saveOrUpdate(p);


sess.flush();
t.commit();
}
sess.close();


ok in order for the book to be saved correctly with person_id being set,

i moved the sess.save(b) up to above p.addToBooks(b); which means like this

b.setPerson(p);
sess.save(b);

p.addToBooks(b);
sess.saveOrUpdate(p);


then i add the newly saved book to the person , then i saveOrUpdate(p)

Only after this sequence of steps, then the person_id will be set in the Book table!!

man, this is really weird!!!! Thanks Vampman for your help!!! though you didnt help me solve problem, but u sure did get me thinking real hard!!! thanks again


and "thanks" hibernate for all the trouble!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.