-->
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.  [ 9 posts ] 
Author Message
 Post subject: Sharing sequences between 2 entities
PostPosted: Thu Jan 11, 2007 7:49 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
[b]Hibernate version:3,2,1,GA

I'm trying to share a sequence between two tables, as EJB3.0 persistence api docs states:

Quote:
This annotation defines a primary key generator that may be referenced by name when a generator element is specified for the GeneratedValue annotation. A sequence generator may be specified on the entity class or on the primary key field or property. The scope of the generator name is global to the persistence unit (across all generator types).


My First Entity has the following anotations
Code:
@Id
@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="services_process_seq")
@SequenceGenerator(name="services_process_seq", sequenceName="services_process_id_seq")
private int id;


In My Second Entity I tried:
Code:
@Id
@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="services_process_seq")
private int id;


Id doesn't work it gives:
Code:
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.AnnotationException: Unknown Id.generator: services_process_seq


Then I tried
Code:
@Id
@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="services_process_seq")
@SequenceGenerator(name="services_process_seq")
private int id;


It gives me this error,(because hibernate_sequence does no exist, of course this is correct I'm expecting to use serices_process_id_seq)
Code:
   select
        nextval ('hibernate_sequence')
ERROR: Could not create data: org.hibernate.exception.SQLGrammarException: could not get next sequence value


I've tried some more variants as writing de @SequenceGenerator at class level, not field/attribute, etc..., and all of them give me the same error.

Can anyone tell me if I'm expecting something that won't happen, I'm writing something wrong ?

Any help will be welcomed
Thanks in advance
tonio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 12, 2007 8:06 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
services_process_id_seq != services_process_seq

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Sharing sequences between 2 entities
PostPosted: Mon Jan 15, 2007 3:18 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Thanks Emanuell,

But this did not resolve the issue, all works the same way as I described in my first e-mail changing the name of both @GeneratedValue's generator attribute and @SequenceGenerator's name attribute to "services_process_id_seq".

Just to make sure that you understand what I need:

If I use the same code for both classes it will work but they are not sharing the generator for example:

Code:
   @Id
   @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false)
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="services_process_id_seq")
   @SequenceGenerator(name="services_process_id_seq", sequenceName="services_process_id_seq")
   public int getId() {
      return this.id;
   }


What will happen is:

Insert in class1
id=51

insert in class2
id=101

new insert in class1
id=52


What I expect to happen is
Insert in class1
id=51

insert in class2
id=52

new insert in class1
id=53


Hope I'm clear enough
Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 11:07 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 2:31 pm
Posts: 25
Location: USA
Try using/setting the increment value to be 1 or try using hilo generator .Here you need to create a table named hi_value.

<id name="losItmId" type="int">
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">1</param>
</generator>
</id>

Thanks,
Vinodh

_________________
I am using a shitty e-mail filtering system that caused a lot of bounces for the admin of this forum. I need to turn on my brain next time and update my e-mail address.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 11:33 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

Try adding the follwing items

Code:
initialValue = 1, allocationSize = 1


In your @SequenceGenerator annotation


Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject: Sharing sequences between 2 entities
PostPosted: Tue Jan 16, 2007 1:11 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Thanks all,

Yes, yes I forgot to mention, I new that changing allocationSize to 1 will solve my problem, but this implies making two calls to the database for every insert in this tables.

Now as de api documentation says, an this is the precise question:

Must we be able to share a sequence between two entities or not, I'm misunderstanding the documentation or is this a feature jboss EJB3.0 persistence is not implementing correctly ?

Thanks again, and I'm stuck to allocationSize=1 until I have some news
tonio


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 21, 2007 7:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
ah, the same sequence is shared, not the same generator,
you could try and implement your own identitygenerator to achieve what you want, but you'll have to rely on some static variable somewhere

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 5:21 am 
Newbie

Joined: Tue Jul 03, 2007 5:06 am
Posts: 1
Code:
@emmanuel
ah, the same sequence is shared, not the same generator,


what's the difference between a shared sequence and a shared generator ?

The SequenceGenerator - Annotation maps a 'name' to a 'sequenceName' - this is a 1:1 relation - therefore if the sequencegenerator is shared the db-sequence has to be shared too ...
http://java.sun.com/javaee/5/docs/api/j ... rator.html

Did i misunderstood the api - doc ?

_________________
--------------
Matthias M.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 11:15 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
For someone not writing a generator strategy there is no difference
For someone write an id generator strategy, it means that several instances of the same generator and for the same set of parameters can exist.

_________________
Emmanuel


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