-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Auto Generator sequence
PostPosted: Wed Sep 21, 2011 6:15 am 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Dear friends,

I want to know that, Is there any way that I can generate an auto generated no at hibernate end using JPA and the same will work on any database e.g. oracle, mySQL, SQL server etc. I don't want to create any sequence on database level. Is this possible at hibernate level only?

I read these strategies but all require me to create a sequence(e.g. for oracle) @ database side.

The Auto Strategy
The Identity Strategy
The Sequence Strategy
The Table Strategy

In above cases, Suppose tomorrow i need to switch the database then i need to rewrite all the POJOS those are using these generator strategies.

Please provide me a solution, I might be wrong to understand the above strategies because I'm quite new hibernate learner.

Thanks


Top
 Profile  
 
 Post subject: Re: Auto Generator sequence
PostPosted: Wed Sep 21, 2011 2:25 pm 
Newbie

Joined: Wed Sep 21, 2011 2:20 pm
Posts: 16
have a look a the "native" stategie. maybe that's, what you are searching for.

http://docs.jboss.org/hibernate/core/3. ... -generator


Top
 Profile  
 
 Post subject: Re: Auto Generator sequence
PostPosted: Wed Sep 21, 2011 11:35 pm 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Hi! qwertz12345

No friend I'm not looking out for native strategy. Native strategy require a sequence_name that is created in database. I'm looking out for the one where a sequence or auto generation should be at Hibernate/Java side only and not on database side. If I switch the database I suppose from oracle to mySQL or vice versa , my code should work on that too without changing the entity classes for auto generation strategies.

Thanks


Top
 Profile  
 
 Post subject: Re: Auto Generator sequence
PostPosted: Thu Sep 22, 2011 1:25 am 
Newbie

Joined: Wed Sep 21, 2011 2:20 pm
Posts: 16
that should work with the native strategie. hibernate is then generating a sequence for you and using it (without you metntioning it an the class definition), if your database is oracle. and it is using another strategie for mysql. so you do not have to change anything. at least it worked for me some time ago in that fashion.


Top
 Profile  
 
 Post subject: Re: Auto Generator sequence
PostPosted: Thu Sep 22, 2011 1:41 pm 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Hi!,
Below is the code through which I achieved the auto generated columns, But I have one serious issue with it and that is, It is generating the sequence incremented by 1 on every consecutive entry like 1,2,3. But If i restart the application server then the next sequence become like something 32768,32769 and if again restart then seq become 65536, 65537. What I want the seq has to start from 1 and increment by 1 whether I restart the server or not.Please provide me some solution on that.

One more thing, I changed the data type of mediaId from BigDecimal to Integer to incorporate @TableGenerator
is this possible GeneratedValue can work with BigDecimal, if Yes please provide a solution.

private Integer mediaId;

@Id
@TableGenerator(name = "MEDIA_TABLE_GENERATOR", table = "MEDIA", initialValue = 0, allocationSize = 1, pkColumnValue = "MEDIA_ID")
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "MEDIA_ID", unique = true, nullable = false, precision = 22, scale = 0)
public Integer getMediaId() {
return this.mediaId;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.