Two Laptop Bag*
The moose likes Object Relational Mapping and the fly likes Horible tiney issue in Hibernate. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Horible tiney issue in Hibernate." Watch "Horible tiney issue in Hibernate." New topic
Author

Horible tiney issue in Hibernate.

Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
I have a simple table .. Student.

colums.
student_id and student_name

here is my code.

Student student=new Student();
student.setId("1");
student.setName("asif");

StudentDAO dao=new StudentDAO(); //Hibernate DAO
dao.saveOrUpdate(student);

I am getting the exception ..............
" Batch update row count wrong: 0"

Anybody know wht the is this ?
regards
A.B
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9960
    
163

Post your hbm file. My wild guess is that you might have specified an unsaved value as 0 or -1 for the id, and in your code you are setting the id as 1 and as a result Hibernate is issuing an update instead of insert statement.


[My Blog] [JavaRanch Journal]
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
check your id generator part. is unsaved value correct ? is it set to assigned ? if not, then you do not need to set an id.
Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
hi guys !
THanks for your replies..
well, I used Student entity as an example. actually i m a Node business objects.. and folliwing is its HBM and java code that causing this exception... hope it will help you Hibernate gurus to find an answer

HMB


Java CODE



THanks
A.B
Sebastian Hennebrueder
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
When you want to assign an id you must select id generator="assigned".

Please read the Hibernate Reference.

Best Regards

Sebastian


Things get always more complicated as expected.<br />Author of eBook Hibernate 3 Developer Guide by example<br />Tutorials about Hibernate, EJB, Struts, JSF <a href="http://www.laliluna.de" target="_blank" rel="nofollow">www.laliluna.de</a>
Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
You are rite ... and I was wrong. I changed it to "assigned"

still same exception.. and i guess it had nothing to do with, wht u said.

Now here is my exception for your consideration .


Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614


Is this new row your trying to insert? what database are you using?
I would suggest use <generator class="org.hibernate.id.IdentityGenerator" />
if your database supports autoincrement (like mysql) and class="sequence" if your databas supports sequence (like oracle) and class="native" if you dont know what your database supports.

and for new rows to be inserted do not set id,let hibernate take care of it.

-Praful
[ May 18, 2006: Message edited by: Praful Thakare ]

All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
Thanks for your post.
But my requirement is not to use Hibernate generated id, and to assign my own application generated id. How can I accomplish this ?

I am using Mysql, and in one other object i m using class="identity" and it is working fine.


Regards
Awais Bajwa
Sebastian Hennebrueder
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
Hello,
I just saw something in your mapping.
In your mapping you are using uppercase names. They should be lowercase.

Best Regards

Sebastian
Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
Thanks again .. i have tried it using the lower case properties..
but again following is the result.
Sebastian Hennebrueder
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
Hello,
the following did work on my system. You might add your columns step by step.




Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
Sebasistian,
Thanks a lot for your coding example, and that coding example points to a fairly simple mistake I made. I corrected it and my problem has been resolved.

I want to write the soloution of my issue , so that all readers get this information.


I used <generator class="assigned" /> for my identity.

and then I used node.saveOrUpdate(node);
So hibernate tried to update that node, (in saveOrUpdate() hibernate always tries to update that record if the id exists.)

when Hibernate tries to update the recorde there is no record already exists in the database, so it throws exception



saveOrUpdate() function wud hav worked if i wud use




for verification you may use saveOrUpdate() in your and you will get the same exception above.

Regards
Awais Bajwa
[ May 24, 2006: Message edited by: Awais Bajwa ]
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
it good your code is working but just for my info

When Sebastian suggested
When you want to assign an id you must select id generator="assigned"

you replied
You are rite ... and I was wrong. I changed it to "assigned"
still same exception.. and i guess it had nothing to do with, wht u said.


are we missing something here?

-Praful
Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
Yeah, when I changed to generator="assigned", I should have used
node.save();

I used node.saveOrUpdate(), instead

that was the think that we were missing.

A.B
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
waoo very useful info,Thanks

-Praful
 
jQuery in Action, 2nd edition
 
subject: Horible tiney issue in Hibernate.
 
Similar Threads
reloading the same page with message - spring mvc
Error while Deploying BMP bean
Not able to Inserting integer data
Practical way to write persistence methods / DAO
errors uploading an image to the database