aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Question" Watch "Hibernate Question" New topic
Author

Hibernate Question

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jul 18, 2001
Posts: 2712
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.
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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 ]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Question