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.
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.
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.
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.
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.
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 ]
Post by:autobot
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