-->
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.  [ 13 posts ] 
Author Message
 Post subject: how to use Postgres Sequence in hibernate ????????
PostPosted: Wed Jan 14, 2009 6:07 am 
Beginner
Beginner

Joined: Sat May 31, 2008 2:35 am
Posts: 30
Location: pakistan
hello

i facing a strange problem in my code.. first little bit explain my DB

i have Postgres DB in which have table Client,..
Client atble have fields like
clientID *sequence
ClientName *Filed
ClientEmail *Primary Key


now in hibernate i want to assign the generator name in the field ClientID

i using in Annotation fr mapping

bt when i do that


@Entity
@javax.persistence.SequenceGenerator(
name="SEQ_Client",
sequenceName="client_clientid_seq",
allocationSize=1)

@Table(name="client")
public class Client implements Serializable,Comparable<Client>{

@Id
private String businessname;

@Column(name="clientid")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_Client")
private Integer clientid;




but it not taking the sequence Generator on the Filed wheich is not a primary Key and not increment the value when i try to save data in Client table...

so help me out how i can set the Sequence Genrator on simple Filed in hibernate

waiting fr response....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 6:18 am 
Beginner
Beginner

Joined: Sat May 31, 2008 2:35 am
Posts: 30
Location: pakistan
i simply want to apply a generator on a Field which is not a Primary key is it possible becase i not seen any example in mapping in which the Sequence gerator is apply accept Primary Key....

kindly Guide me is that possibly to apply the Sequence Gerator on simple Field

waiting fr response


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 7:40 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
Nommi wrote:
i simply want to apply a generator on a Field which is not a Primary key is it possible becase i not seen any example in mapping in which the Sequence gerator is apply accept Primary Key....

kindly Guide me is that possibly to apply the Sequence Gerator on simple Field

waiting fr response


Standard hibernate/jpa generators are applied only to the entity identifiers.

I see at least two ways to go here:
    Make your 'clientId' to be entity id and add if necessary UNIQUE constraint for the email column;
    Introduce a db-level trigger that fills client id on new record insert;


The first option is much more preferable either for performance or db design characteristics.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 7:51 am 
Beginner
Beginner

Joined: Sat May 31, 2008 2:35 am
Posts: 30
Location: pakistan
hmmmm same thing i am doing now i change the primary key client id and set unique on businessEmail... but it is not a sulotion..... is there any way i use same mapping in hbm.xml file i mean doing same sequence in hbm files on non Primary key... if is it than show me the example how to set in hbm files..

thanx fr quick res...

can u tell me how that i can do in Postgres
Introduce a db-level trigger that fills client id on new record insert;



still waiting to resolve it


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 8:33 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
Nommi wrote:
hmmmm same thing i am doing now i change the primary key client id and set unique on businessEmail... but it is not a sulotion..... is there any way i use same mapping in hbm.xml file i mean doing same sequence in hbm files on non Primary key... if is it than show me the example how to set in hbm files..


That is a solution. As I said before, orm generators work only with the entity ids, hence if you have a client id to be an entity id you can apply any suitable generator strategy. Also synthetic id looks like more convenient and efficient as a primary key than the user email.

Nommi wrote:
can u tell me how that i can do in Postgres
Introduce a db-level trigger that fills client id on new record insert;



still waiting to resolve it


Chapter 35. Triggers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 3:53 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Nommi wrote:
...i change the primary key client id and set unique on businessEmail... but it is not a sulotion.....


I'm interested, why is that not a solution?
A unique email adress would qualify as a fine natural key, but how do you reference your user in other tables? by email adress or by clientid?

The former would be a rather bad idea usually...

just my 0.02€


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 12:55 am 
Beginner
Beginner

Joined: Sat May 31, 2008 2:35 am
Posts: 30
Location: pakistan
sorry fr late rep i leave the office after my last reply...
hmmmmmm ok i agree with u abt the Primary key as a Client id...

bt still different question why it is a limitation in hibernate that we only set the Sequence only on Primart key... is there no alternative abt it... there are many situation in which we wanna use sequence on any field in Db so if use Hibernate than thats mean stuck on it....

ok one more point i got
if i simply run query and wanna add user than DB sequence work and increment bt when i try to save using Hibernate session.save(obj); than my filed value set to be null and not increment..

so there is only one solution that we run Custom made query from hibernate and not give filed (ClientID) it will automatically increment in DB side... so any alternative to reduce writing query headache


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 4:17 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Nommi wrote:
bt still different question why it is a limitation in hibernate that we only set the Sequence only on Primart key... is there no alternative abt it... there are many situation in which we wanna use sequence on any field in Db so if use Hibernate than thats mean stuck on it....


Don't know, but you can always do this in the database i guess.
Add a trigger that works on insert.

Maybe it's considered questionable design if you have another (unique) value in the same table thats assigned by sequence?
Because if you have one unique value you wouldn't need the other as primary key in the first place...

Nommi wrote:
if i simply run query and wanna add user than DB sequence work and increment bt when i try to save using Hibernate session.save(obj); than my filed value set to be null and not increment..

Code:
User user = new User();
user.setName("Myname");
session.save(user);

works fine for me with ID and GeneratedValue from Sequence


Top
 Profile  
 
 Post subject: Same problem...
PostPosted: Wed Feb 04, 2009 2:59 pm 
Newbie

Joined: Wed Feb 04, 2009 2:52 pm
Posts: 3
Did you found a way to use the hibernate sequence generator on a simple field? I didn't found any example about it. Do you change the primary key as they suggested?
[]'s


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 5:06 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Map your property with generated="insert" and insert="false". Eg.

Code:
<property
  name="someName"
  column="someColumn"
  generated="insert"
  insert="false"
  ... etc.
/>


And then when you create your database table (PostgreSQL):

Code:
CREATE TABLE SomeTable (
    someColumn integer DEFAULT nextval('someSequence'),
    ... etc.
);


Top
 Profile  
 
 Post subject: Resolved!
PostPosted: Thu Feb 05, 2009 8:24 am 
Newbie

Joined: Wed Feb 04, 2009 2:52 pm
Posts: 3
Nordborg, I'm using annotations but I resolved my problem with your post/help. I wasn't using "insertable=false" on @Column annotation.
Here is the code:

Java file:
Code:
...
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "notas_not_id_seq")
   @Column(name = "not_id", insertable = false)
   private int id;
...


SQL:
Code:
not_id serial NOT NULL

CREATE SEQUENCE notas_not_id_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;


Thanks.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Mon Aug 02, 2010 7:22 am 
Newbie

Joined: Tue Oct 06, 2009 1:40 pm
Posts: 10
nordborg wrote:
Map your property with generated="insert" and insert="false". Eg.

Code:
<property
  name="someName"
  column="someColumn"
  generated="insert"
  insert="false"
  ... etc.
/>


And then when you create your database table (PostgreSQL):

Code:
CREATE TABLE SomeTable (
    someColumn integer DEFAULT nextval('someSequence'),
    ... etc.
);


I have just declared

Code:
    <property name="code" generated="insert" insert="false"/>


and code is declared as Integer.
schema is generated via ddl, and there is no autoincrement flag set, and in the end, no number is ever generated.
What am I missing?


Top
 Profile  
 
 Post subject: Re: how to use Postgres Sequence in hibernate ????????
PostPosted: Mon Aug 02, 2010 9:21 am 
Newbie

Joined: Wed Feb 04, 2009 2:52 pm
Posts: 3
How is the SQL code for your autoincrement field?


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