• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Hibernate Question

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chris Mathews
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg,
.... this whole lurking thing is so much easier when people think loud like you.
thanks
[ June 10, 2003: Message edited by: Axel Janssen ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic