-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to make a field as an Autogenerated
PostPosted: Mon Sep 07, 2009 4:01 am 
Newbie

Joined: Fri Sep 04, 2009 10:04 am
Posts: 10
Hi, can any one help me in making a field as an auto increment, I am using my application as to support all three database My-SQL, Postgres, and Oracle


Top
 Profile  
 
 Post subject: Re: How to make a field as an Autogenerated
PostPosted: Mon Sep 07, 2009 6:17 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
may be

<generator class="native"/>
It picks identity, sequence or hilo depending upon the capabilities of the underlying database.

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
 Post subject: Re: How to make a field as an Autogenerated
PostPosted: Mon Sep 07, 2009 6:19 am 
Newbie

Joined: Fri Sep 04, 2009 10:04 am
Posts: 10
I need Using JPA, I solved that

@SequenceGenerator(name="objectscope",sequenceName="object_scope_sequence")
@GeneratedValue(generator="objectscope")


Top
 Profile  
 
 Post subject: Re: How to make a field as an Autogenerated
PostPosted: Mon Sep 07, 2009 6:21 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
Awesome!

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
 Post subject: Re: How to make a field as an Autogenerated
PostPosted: Mon Sep 07, 2009 7:41 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
javax.persistence.GenerationType.Sequence

Some database vendors support the use of a database sequence object for maintaining primary keys. To use a sequence, you set the GenerationType strategy to SEQUENCE, specify the name of the generator annotation, and then provide the @SequenceGenerator annotation that has attributes for defining both the name of the sequence annotation, and the name of the actual sequence object in the database.

Here's what the getId() method of the Snafu class would look like if we used a SEQUENCE GenerationType:


Code:
@Id
@SequenceGenerator(name="s1", sequenceName="SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s1")
public long getId() {return id;}


http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=12oneclasstoonetablejpamapping

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: How to make a field as an Autogenerated
PostPosted: Mon Sep 07, 2009 7:49 am 
Newbie

Joined: Fri Sep 04, 2009 10:04 am
Posts: 10
I hope GenerationType.SEQUENCE is only for oracle, it will not be help full for MySQL and others, at that time we should write the conditioanl based code, if we use
Code:
@GeneratedValue(generator="objectscope")
, it should works for all DBS, I tested in MYSQL. Oracle and POSTGres


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.