1 Reply Latest reply on Sep 23, 2008 8:39 AM by nickarls

    Creating a sequence for a nonprimary key property

    zzzz8.zzzz8.netzero.net

      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.



      @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:


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



      I also create a sequence in the script:


      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, PostGres 8.1, and JBoss 4.2.2 GA.