Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Horible tiney issue in Hibernate.

 
Awais Bajwa
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you want to assign an id you must select id generator="assigned".

Please read the Hibernate Reference.

Best Regards

Sebastian
 
Awais Bajwa
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Awais Bajwa
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again .. i have tried it using the lower case properties..
but again following is the result.
 
Sebastian Hennebrueder
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
the following did work on my system. You might add your columns step by step.




 
Awais Bajwa
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
waoo very useful info,Thanks

-Praful
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic