Forums Register Login

Hibernate Question

+Pie Number of slices to send: Send
Ok, I am at my wits end trying to get this simple Hibernate example to work. I am trying to save() data to the database and when I use one of the standard identifier algorithms I get the following error from the MySQL Database:
Duplicate entry '0' for key 1.
I know what that means. The Hibernate example is not generating a new Unique ID like it is supposed to be doing. Below are all my files
Users.hbm.xml

Users.java

HiberTest.java

If anyone could give me some insight on this I would really appreciate it. If this needs to be moved to a more appropriate forum, please do so.
Thanks.
+Pie Number of slices to send: Send
 

Originally posted by Gregg Bolinger:
The Hibernate example is not generating a new Unique ID like it is supposed to be doing.


Actually, since you are using the identity generator, it is really up to MySQL to generate the unique id. Hibernate is just going to retrieves its value. Therefore, the next question is: Did you set up LogonID as an identity field in your database schema?
For testing purposes you could also use the hilo generator.

For this to work you will need an additional table in your schema named User_Identity with a single column named next. Unfortunately, you can't use hilo when supplying your own Connection to Hibernate, or when Hibernate is using an application server datasource to obtain connections enlisted with JTA. Therefore it is best when used in testing/prototyping, in a real production application your best bet is to delegate the id generation to the underlying database with either an identity field or sequence depending on what your database supports.
+Pie Number of slices to send: Send
Thanks for the response Chris. Well, since I use MySQL sequence is out of the question. Is there anything wrong or should I say is it bad practice to just setup the LogonID as an autoincremented field instead of messing with identity/sequence/native stuff?
I guess that might depend on what I needed the LogonID to do. In my example, it is simply a unique identifier that cannot be duplicated. The only reason I can think of not to use an autoincremented field is if I needed a custom identifier, but then I would have to write my own identifier algorithm anyway.
+Pie Number of slices to send: Send
 

Originally posted by Chris Mathews:
[QB
Did you set up LogonID as an identity field in your database schema?
[/QB]


Could you give me some more info on this? I did set it as the Primary Key. What else do I need to do to it?
+Pie Number of slices to send: Send
Ok, I believe that I have figured this out. In all the Hibernation documentation they were using some syntax in there create table statement where they said "generated by default as identity". MySQL doesn't understand this command, but the equevilent in MySQL is to use the AUTO_INCREMENT command.
Once I did that, it started working. So that is how I make my column the "indetity" column. Damn non-standards. Anway, Chris, thanks for your help. I think I am off and running for a while anyway. We'll see what else I can screw up.
+Pie Number of slices to send: Send
 

Originally posted by Gregg Bolinger:
Ok, I believe that I have figured this out. In all the Hibernation documentation they were using some syntax in there create table statement where they said "generated by default as identity". MySQL doesn't understand this command, but the equevilent in MySQL is to use the AUTO_INCREMENT command.
Once I did that, it started working. So that is how I make my column the "indetity" column. Damn non-standards. Anway, Chris, thanks for your help. I think I am off and running for a while anyway. We'll see what else I can screw up.


This whole "helping" thing is soooo much easier when people answer their own questions like you.
+Pie Number of slices to send: Send
Gregg,
.... this whole lurking thing is so much easier when people think loud like you.
thanks
[ June 10, 2003: Message edited by: Axel Janssen ]
+Pie Number of slices to send: Send
 

Originally posted by Axel Janssen:
Gregg,
.... this whole lurking thing is so much easier when people think loud like you.
thanks
[ June 10, 2003: Message edited by: Axel Janssen ]


Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1471 times.
Similar Threads
Incorrect syntax near the keyword
Trouble running example from Hibernate Made Easy, p. 156
how to update OneToMany
One to many Relation
Hibernate Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:45:01.