-->
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.  [ 1 post ] 
Author Message
 Post subject: Creating a sequence for a nonprimary key property
PostPosted: Tue Sep 23, 2008 4:50 pm 
Newbie

Joined: Mon Sep 22, 2008 8:28 pm
Posts: 2
I need to create a sequence for a property (that is not a primary key or part of a primary key) in my entity bean. It has the following requirements:

    If the field for this entity is set to null via the setter method and the entity is persisted, then a sequence number must be auto generated for this field. This new sequence number must be unique - i.e. must not replicate the sequence number of any other entity in the database.
    If the field for this entity is nonnull - i.e. set via the setter method, the field takes the value of that is specified as an argument in the setter method.


I'm trying to accomplish this in my entity bean code right now, but it doesn't seem to work.

Code:
@Entity
@Table(name = "store")
public class Store implements java.io.Serializable {

...

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "StoreIdSequenceGenerator")
   @SequenceGenerator(name = "StoreIdSequenceGenerator", sequenceName = "store_storeid_seq")
   @Column(name = "storeid", nullable = false)
   public int getStoreid() {
      return this.storeid;
   }

...

}


As such, I'm attempting to use the JPA sequence generation facilities. Here's my table script:

Code:
CREATE TABLE store (
...
      storeId SERIAL NOT NULL,
...
)


I also create a sequence in the script:

Code:
ALTER SEQUENCE store_storeid_seq INCREMENT BY 1;


Again, storeId is not a primary key. The store table may have one or more records that contain the same storeId (the records are used in a way such that it may contain different recorded versions of a store, much in the way Wikipedia stores different versions of wiki entries). That's why storeId is not a primary key. I actually have another column that is a primary key column.

How can I do this? Perhaps I am looking something that is very simple. I would really like to avoid making a native query when I perform a persist. Is there anything in Seam that may help me with this issue?

I am currently using Seam 2.0.2 GA (with the Hibernate version bundled with it), PostGres 8.1, and JBoss 4.2.2 GA.


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

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.