Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes sequence generator with mysql 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 "sequence generator with mysql" Watch "sequence generator with mysql" New topic
Author

sequence generator with mysql

indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
Hi

please give me an idea to this newbee to hibernate ..

and when I try to print the generated id from the student, is displaying NULL.

If I add <param..> tag
it ends up with
please help me ...


thanks ,
Indu
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Can you post us the table's DDL?


If you are not laughing at yourself, then you just didn't get the joke.
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
Thanks for your reply... I googled that MySql does not support 'sequence generator '. So I changed the generator class to
increment
then
no exception is coming,but the generated hibernate sql query is with stid = null

and if the generator class is
native
then with stid = null

my mapping file is

Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Please remove, unsaved-value="null" from the hbm file.
You still haven't posted the DDL of the table and can you tell us what is the java type of the id parameter in your class?
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
my DB table ::



in RegisterStudent class the java type of Id is "String"..
I removed unsaved="null" , still the id is not creating in DB..
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Please change your id column type to integer and set auto_increment.
Also change your parameter type in hibernate to Integer.

If you still want to use String as your primary key, you may need to specify the value of the id parameter
in the java object before saving.
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
thanks Arun, it works fine with int datatype ...

I have 1 doubts..

1 . Is it necessary to declare the studID as
autoincrement
in Mysql ? We are already using generating class in Hibernate, then why it is needed in DB the declaration again ?

If I declare as String , what is parameter type we should use ?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

indu iyengar wrote:

1 . Is it necessary to declare the studID as
autoincrement
in Mysql ?


Not necessary. auto_increment is just to tell the database to increment the Primary key identifier value on each new insert.
StudId can be anything, it can be a string, a UUID.

indu iyengar wrote: We are already using generating class in Hibernate, then why it is needed in DB the declaration again ?

generator tag would only tell the hibernate framework what kind of generation strategy we are using. Is it generated by the database
or is it generated by the user or should hibernate fetch the maxid of that column and increment it by one and insert it, etc.,

indu iyengar wrote: If I declare as String , what is parameter type we should use ?

-- Generally, String in java should be equivalent to varchar, char in DB.
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
sorry for the late reply....
Arun, thanks for your reply.. With out using 'auto increment' in mysql , the id is not creating... and the sql error is ..


Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Exactly, which only tells you that your id should be supplied by you when trying to insert.
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
yes.. I understand ,but what I want is to generate the ID by its own while registering the user itself , like Bank Account Number .
I suppose the generator tag in hibernate will do that. , but even by using that tag the ID is not creating by itself.
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Again, generator tag will not guarantee any auto-generation of keys. As I said earlier, generator tag only hints the hibernate what kind of generation strategy is employed for the id column in the table. If it is auto-generated then we need to make it "auto_increment" in the table, if we don't want to have it auto-generated then we mention it as "assigned" or any other hibernate specified format.
Using native -- selects identity, sequence or hilo depending upon the capabilities of the underlying database.
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
I set the default value in Mysql as 0, now the error has gone, but value not inserting.
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
please can anyone help me in solving this ...

with session.save() the object is not persisting into the database..

the hibernate generated query when I run is -- .>>
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 537

Hi Indu,
Try closing the session object, its not closed. Use session.close().
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
am closing it in finally block..
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 537

Hi Indu,
Try flushing the session after the transaction is committed... Also, can you post here the full function used for inserting into database.
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 537

Did you try committing the transaction before flushing the session. If yes, can you try with setting auto commit in hibernate.cfg.xml to true.. Because, the system is not able to commit the data or data is already flushed out..
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Setting to auto_commit should solve the issue. But the OP should really understand transactions and flushing.
indu iyengar
Ranch Hand

Joined: Jul 30, 2010
Posts: 115
thanks guys ... you helped a lot in fixing the issue
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sequence generator with mysql