Sequence generated keys in Apex

In APEX, what is the best way to handle INSERTs when the primary key is generated from a SEQUENCE (triggers not allowed).
Thanks,
Mike

Hi,
How trigger will harm that ?????
You trigger could be like
create or replace
TRIGGER MY_TBL_TRG_TRG
  BEFORE INSERT ON MY_TBL
  FOR EACH row
BEGIN
  IF inserting THEN
    IF :NEW.PK_ID IS NULL THEN
      SELECT MY_TBL_PK__SEQ.nextval
      INTO :NEW.PK_ID
      FROM dual;
    END IF;
  END IF;
END;If you insert table and supply PK value, it would not generate it from sequence.
You should check shop rules.
Regards,
Jari

Similar Messages

  • Knowing the Primary Key Sequence Generator of a table

    Hello All,
    I'm working on an application that uses Oracle toplinks/JPA for persistence. The entity classes in the application have primary key fields with annotations @Id and @GeneratedValue(strategy = GenerationType.AUTO). Per our understanding, this means the primary key generation is done by JPA/Top-links. When we see our database, we notice only one SEQUENCE table available and that table has only one row, while in the application, we are getting different sequence numbers for the primary key of each entity. So, not clear on how this sequence number is generated. Could anybody share information on where the sequence number is stored for each entity/table? Really appreciate your help here. Our database is MSSQL.
    Thank you,
    Venkatesh

    EclipseLink/TopLink uses Table sequencing for GenerationType.AUTO because it is more portable between databases. My understanding of your issue is from
    http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Table_sequencing
    Each row in the table represents one sequence generator, and your entities are all using the same default sequence generator and so all use the same row.
    To use multiple rows I believe you need to define multiple TableGenerators.
    Best Regards,
    Chris

  • Java persistence: sequence generator not working in expected way.

    Hi,
    I just played around with java persistence api and found that sequence generator annotations for making new primary key value not working correctly in my code. following is part of my code related to this problem.
    in entity class:
    @SequenceGenerator(name="mySeq", sequenceName="MY_SEQ_IN_DB")
    @Id
    @GeneratedValue(strategy=SEQUENCE, generator="mySeq")
    @Column(name = "UPLOAD_ID", nullable = false)
    private Integer uploadId;
    I have a sequence called MY_SEQ_IN_DB in my oracle 9i testing db. When I ran the code to save a new entity instance to database I found that the new upload id is not the one that should be created from the MY_SEQ_IN_DB sequence, while the sequence was not increased either. It's instead something starting from 97 (in my pc), and increased just like a sequence, but every time I reran my code the sequence restarted itself again beginning with 97(of course the insert failed with primary key conflict). the behavior just looks like it's a sequence created and managed totally by the persistence api or toplink itself (I guess).
    So, my question is, is this a designed behavior or have I done something wrong to make it not using the correct sequence in the db?
    I used hibernate before, and assigning new primary key from sequence in this way works fine.
    Thanks
    JC.L

    There are some things in Java which can't be serialized. When contained within a higher-level class such as Buttons. A button can be associated with an Action Listener. Now, you can serialize a button; however you can't serialize an action listener - for what should be to you obvious reasons. This is NOT an error and so there is no need to throw an exception when serializing the button.
    Basically: Applet sends servlet button, but not listener.

  • JPA: Oracle Sequence Generator not up to date

    Hi,
    I'm using the JPA Oracle Sequence Generator in one of my JPA classes:
    @Entity
    @Table(name = "DACC_COST_TYPE")
    public class JPACostType implements Serializable {
    @SequenceGenerator(name = "CostTypeGenerator", sequenceName = "DACC_COST_TYPE_SEQ")
        @Column(name = "ID_COST_TYPE")
        @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CostTypeGenerator")
        private Integer idCostType;
    In order to persist a new object I perform the following code:
    @PersistenceContext
    private EntityManager em;
    JPACostType myJPA = new JPACostType();
    myJPA.setIdCostType = null;
    em.merge(myJPA);
    em.flush();
    Normally this works fine. But after deploying the app there sometimes happens an error:
    Caused by: javax.persistence.PersistenceException: SQLException while inserting entity {com.karmann.dacc.ejb.busilog.jpa.JPACostType(idCostType=4)}.
    at com.sap.engine.services.orpersistence.core.PersistenceContextImpl.flush(PersistenceContextImpl.java:278)
    at com.sap.engine.services.orpersistence.core.PersistenceContextImpl.beforeCompletion(PersistenceContextImpl.java:565)
    at com.sap.engine.services.orpersistence.entitymanager.EntityManagerImpl.beforeCompletion(EntityManagerImpl.java:410)
    at com.sap.engine.services.orpersistence.environment.AppJTAEnvironmentManager.beforeCompletion(AppJTAEnvironmentManager.java:197)
    at com.sap.engine.services.ts.jta.impl.TransactionImpl.commit(TransactionImpl.java:232)
    ... 52 more
    Caused by: java.sql.SQLException: ORA-00001: unique constraint (AEMA.DACC_COST_TYPE_PK) violated
    Obviously JPA does not fetch the new key by accessing the Oracle sequence. This documents "next value = 5". Does JPA fetch the new key from its cache? Is there any possibility to avoid this?
    Thanks for each hint,
    Christoph

    Hello Christoph Schäfer  ,
    I am stuck with a similar issue. I was able to save mutiple entries and there has not been much change to my JPA. I added new entities and new sequences.
    Now, I get the error Caused by: javax.persistence.PersistenceException: java.sql.SQLException: ORA-02289: Sequence ist nicht vorhanden.
    I have checked the name of sequence and sequence next val on the DB. It works on DB but when i execute it from ejb, it gives me thsi error. Now, it gives the error for all previously working JPA entities.
    I have also provided allocationSize = 1 for all entities.
    Please let me know, possible cause/solution to this issue.
    thank you.
    Regards,
    Sharath

  • Sequence Generator problem in OWB Mapping

    Hi,
    I am using a Sequence Generator in my Dimension mapping. And OWB implements the mapping via an Oracle MERGE statement.
    However when I execute the mapping my sequence number value gets incremented even when there are only updates and no inserts taking place during the execution.
    I am using the sequence number only for inserts and not for updates in the mapping.
    Is there a way to avoid this situation or a work around to this problem? This is very urgent and any help in this area would be greatly appreciated.
    Thanks

    Hi
    I also have the same problem as I loose the seq numbers when there are updates only and I do not want to loose thousands of seq numbers. I tried with bulk size =1 as well as row based only execution but no result.
    The thing which solved this problem is to have outer join with source and target, then use splitter to find out new and updated records (by looking the join key as null on target table side), then use a fuction to generate seq number when the key is not null. But doing all this does not seem to be elegant performance point of you. I do not want to join source and target due to performance issues.
    Could you pl clrify your apprach. Are you using , Source minus target and Source Intersect Target . What you will do if there is no one to one matching between source and the target.
    Thanks

  • Support for Generated Keys in oracke jdbc drivers?

    Hi All,
    Anyone know when the Oracle Thin JDBC drivers are going to support generated keys?
    Regards,
    Lee

    Sure Justin,
    I should have made myself more clear. Sorry about that.
    I have a sequence for generating primary keys in a particular table, and a trigger to replace a null in my insert query with the nextval. That all works fine. I would like to use the DB-neutral means of getting a generated primary key...
    prepStmt = connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
    prepStmt.executeUpdate();
    ... and later ...
    ResultSet generatedKeyRS = prepStmt.getGeneratedKeys();
    ... etc.
    I am aware that I can get this information several other Oracle9i-specific ways, including using the sequence's .currval and creating a callable statement that returns the primary key column. However I would still like to use the nice, neat, vendor-neutral JDBCv3 way. Call me pedantic.
    Regards,
    Lee

  • Sequence generator

    Hi All,
    I am populating a dimension table with a primary key field populated from a sequence generator. The target table is on a SQL Server database and from the literature online, it appears a sequence generator object does not exist in S Server. How do I work around this ? I have tried to create a sequence object through ODI but Im not clear on how to call this sequence generator from the interface. Is this the right approach to populating this sequence fiels ? Any ideas ?
    Thanks
    MN

    Hi MN,
    Look into this link http://odiexperts.com/?p=450
    the other way is to using an variable and row number .
    In variable call like this ' SELECT NVL(MAX(<SEQUENCE_COLULMN_ID>,0)) FROM TARGET_TABLE
    In the interface call like this #VARIABLE+ROWNUM and call the variable in refresh mode and then the Interface.
    This way once the variable fetches the max value, using rownum for the number of insert we can increment the number .
    This method is for Oracle now dealing with SQL Server , i think you can make use of ROW_NUMBER() OVER (ORDER by COLUMN ) for rownum. You can google and learn more about it .
    I have personally not tried it sql server so i am not sure how much help row_number will be.
    Hope this helps.

  • Can a sequence generate negative numbers?

    Hi
    Thank you for reading my post
    i think my sequence generate negative numbers , is it possible ?or somewhere in my application is incorrect ?
      CREATE SEQUENCE  "HR"."GLOB_SEQ"  MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 5 NOCACHE  ORDER  NOCYCLE ;is there anything that i made wrong which caused the negative numbers in my edit box ?
    type of my category_ID is number.
    Thanks

    Generally speaking yes you can generate negative numbers:
    SQL> create sequence seq1 increment by -1;
    Sequence created.
    SQL> select seq1.nextval from dual;
    NEXTVAL
    -1
    SQL> /
    NEXTVAL
    -2
    But if you have defined minvalue as 1 it is impossible because:
    SQL> create sequence seq1 increment by -1 minvalue 1 maxvalue 1000000 start with 1;
    Sequence created.
    SQL> select seq1.nextval from dual;
    NEXTVAL
    1
    SQL> /
    select seq1.nextval from dual
    ERROR at line 1:
    ORA-08004: sequence SEQ1.NEXTVAL goes below MINVALUE and cannot be instantiated
    SQL>
    So most probably someone simply inserted a row with negative values knowing that they'll never be in conflict with sequence generated values, or your code is buggy :)
    Generally for surrogate keys (assuming this is your case) you don't care what values they have.
    Gints Plivna
    http://www.gplivna.eu

  • CMP PK Sequence generated by Bean Self Cache !!

    Hi All,
    In WL6.1, one can mention the name of the sequence and the DB in the cmp-rdbms-jar.xml
    to use the sequence generated number to use as a primary key.
    Though my work is being done by the WL server but still WL server is hitting the
    DB for each insert.
    There are a couple of discussions listed on theserverside.com, but most of them
    fail for a clustered environment.
    Can anyone please suggest me a solution where the WL server would cache the keys
    instead of hitting the DB for each insert ?
    Thanks in advance for your help !!
    Regards
    -Rais

    Hi,
    I performed a test and ran 3 scenarios(inserting 1000 records).
    1. SEQUENCE INCREMENT BY 10 And key-cache-size = 1.
    Time taken = 13 seconds
    Records were inserted as 1,11,21,31.....
    2. SEQUENCE INCREMENTED BY 10 And key-cache-size = 10
    Records were inserted as 1,2,3,4,5...
    Time = 8 seconds
    3. SEQUENCE INCREMENTED BY 1 And key-cache-size = 10
    I got the duplicate primary key problem.
    The behaviour in case 2 is ambiguous to me...I was expecting the records to be
    inserted as 1,11,21,31.....
    Can anyone please clarify ??
    Thanks
    -Rais
    "Rais" <[email protected]> wrote:
    >
    Hi Rob,
    The weblogic Docs and the DTD says that "key-cache-size" should be equal
    to the
    INCREMENT VALUE of the Sequence in DB.Lemme point out what I have interpreted
    from that.Please correct me if I am wrong.
    lets say my sequence is
    CREATE SEQUENCE TEMP_SEQ INCREMENT BY 2.
    And my key-cache-size value is also 2.
    I believe that by keeping these two values equal,records would be inserted
    into
    the DB as 1,3,5,7......
    But in a clustered environment,each CMP would still be hitting the DB
    to get the
    sequence number.
    What I mean to say is key-cache-size is actually not meant for caching
    the sequence
    numbers.
    Even if it is ,how would the servers in the cluster maintain the same
    state of
    their individual cache same ??
    Regards
    -Rais
    Rob Woollen <[email protected]> wrote:
    Did you set a key-cache-size in your CMP deployment descriptor?
    -- Rob
    Rais wrote:
    Hi All,
    In WL6.1, one can mention the name of the sequence and the DB in thecmp-rdbms-jar.xml
    to use the sequence generated number to use as a primary key.
    Though my work is being done by the WL server but still WL server
    is
    hitting the
    DB for each insert.
    There are a couple of discussions listed on theserverside.com, butmost of them
    fail for a clustered environment.
    Can anyone please suggest me a solution where the WL server would
    cache
    the keys
    instead of hitting the DB for each insert ?
    Thanks in advance for your help !!
    Regards
    -Rais

  • Eclipselink Custom Sequence generator

    Hi,
    I am using oracle database. All my tables uses sequence generator. One of my table customer has a sequence generator but needs a prefix for the primary key. If the sequence generated is 01 , I would like it to be saved as C01. Is there anyway I can use database sequence generator and add a prefix for the ID in eclipselink.
    Any help is really appreciated.
    Thanks.

    See,
    http://www.eclipse.org/forums/index.php?t=msg&th=205307&start=0&S=6619f8634cb716a45f2bdaf577719d00

  • OWB's Sequence Generator

    Is there an option within OWB's Sequence Generator to reset the start value to '1' each time we run it. I know there is an option where we can give the start value and increment value but i did not see an option to reset the start value each time we run it.Our requirement is to run the same mapping daily and when we deploy the sequence generator in OWB it is picking up the last value for the surrogate key and assiging the next value when the mapping is run the next day. Please suggest'
    - Note Reset is necessary

    Hi,
    We use what Carsten also suggested, a pre-mapping procedure call. Our procedure is called RESET_SEQUENCE. In the mapping I have a constant operator with 2 output fields: the sequence generator name and a start value. These 2 ports are connected to the pre-mapping process operator with the same 2 input ports.
    The function in the pre-mapping process operator properties is simply and only the name of the procedure: reset_sequence.
    This is working fine for us. Every time the mapping is run, the sequence is reset to 1.
    CREATE OR REPLACE PROCEDURE reset_sequence (seq_name IN VARCHAR2, startvalue IN INTEGER)
    AS
    cval INTEGER;
    inc_by VARCHAR2(25);
    BEGIN
    EXECUTE IMMEDIATE 'ALTER SEQUENCE ' ||seq_name||' MINVALUE 0';
    EXECUTE IMMEDIATE 'SELECT ' ||seq_name ||'.NEXTVAL FROM dual' INTO cval;
    cval := cval - startvalue + 1;
    IF cval < 0 THEN
    inc_by := ' INCREMENT BY ';
    cval := ABS(cval);
    ELSE
    inc_by := ' INCREMENT BY -';
    END IF;
    EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || seq_name || inc_by || cval;
    EXECUTE IMMEDIATE 'SELECT ' ||seq_name ||'.NEXTVAL FROM dual' INTO cval;
    EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || seq_name || ' INCREMENT BY 1';
    END reset_sequence;
    Hope this helps,
    Regards,
    Ed

  • Auto Sequence Generator

    I create a table with a key and anytime I create a new row I
    would like the the key to be updated by one. I want a sequence
    generator to kick off without me doing a nextval. Does anyone
    have a specific example.

    As an example, we consistently uses sequences and triggers of
    the following type. And then we never bother about creating the
    primary key in inserts.
    CREATE SEQUENCE Q_BILL_OF_MATERIALS
    INCREMENT BY 1
    NOMAXVALUE
    NOMINVALUE
    NOCYCLE
    NOCACHE
    CREATE OR REPLACE TRIGGER BOM_INSERT_ID
    BEFORE INSERT
    ON BILL_OF_MATERIALS
    FOR EACH ROW
    WHEN (new.id IS NULL)
    BEGIN
    SELECT     q_bill_of_materials.nextval
    INTO     :new.id
    FROM     dual;
    END;

  • Getting generated keys inside Java

    Is there any way to get generated keys in Java running on CF
    server?
    I have some java code accessed from CF, I get a connection to
    the data source by name via
    "coldfusion.server.DataSourceService.getDatasource(datasourceName).getConnection()"
    call within java, which works. However if I try to do a
    connection.prepareStatement(sql,
    PreparedStatement.RETURN_GENERATED_KEYS) call or
    preparedStatement.getGeneratedKeyts() call I get a
    "java.lang.AbstractMethodError" exception like:
    java.lang.AbstractMethodError:
    coldfusion.server.j2ee.sql.JRunConnectionHandle.prepareStatement(Ljava/lang/String;I)Ljav a/sql/PreparedStatement;
    Which seems to indicate to me that CF does not implement
    getting generated keys via JDBC - is there another way or do I have
    to just give up and do a "select" after "insert"?
    Hope this made sense.
    Thanks,
    -HH
    P.S. In case this is usefull, CF MX7 with MSSQL2005 , JDK
    1.4

    Hichhiker wrote:
    > Is there any way to get generated keys in Java running
    on CF server?
    CF6/7 uses JDBC 2 so I don't think you can get it to work
    there. In CF 8
    it should work (if your database supports it).
    Jochem
    Jochem van Dieten
    Adobe Community Expert for ColdFusion

  • Clarification on the limitation of sequence generator in T-SQL

    Hi, I found this limitation of sequence numbers for T-SQL:
    "If there are multiple instances of the NEXT
    VALUE FOR function specifying the same sequence generator within a single Transact-SQL statement, all those instances
    return the same value for a given row processed by that Transact-SQL statement. This behavior is consistent with the ANSI standard."
    From this link:  http://technet.microsoft.com/en-us/library/ff878058.aspx
    Does this mean that if we want to have different value generated then we can't use it in multi-threading environment whereby multiple instances / processes are calling the same T-SQL code that generate the sequence numbers using the
    NEXT VALUE FOR function?
    My understanding of the above might not be correct, any sample example to illustrate this limitation? 
    Thanks

    Hi
    This is the meaning of the comment in the article.
    If you are using in a single t-sql
    statement several times (NEXT VALUE FOR) then it will act as you use the same value. This is in-design :-)
    try this for example:
    select (NEXT VALUE FOR MySequenceName),(NEXT VALUE FOR MySequenceName),(NEXT VALUE FOR MySequenceName) -- same value several times
    But you can use (NEXT VALUE FOR) several time like this:
    DECLARE @QQ_01 int, @QQ_02 int, @QQ_03 int, @QQ_04 int
    SET @QQ_01 = NEXT VALUE FOR MySequenceName;
    SET @QQ_02 = NEXT VALUE FOR MySequenceName;
    SET @QQ_03 = NEXT VALUE FOR MySequenceName;
    SET @QQ_04 = NEXT VALUE FOR MySequenceName;
    SELECT @QQ_01,@QQ_02,@QQ_03,@QQ_04 -- different valus
    Hope this clarify things :-)
    [Personal Site] [Blog] [Facebook]

  • How to set a sequence generator as a value

    When i am creating a new object in a table there is a unique id i want to keep to identify this object and I use sequence generator. I believe the command is DB_SEQ_PK.NEXTVAL, do i put this statement as a "SQL EXP" in the Source value for the specific variable i want to set it to? Please give me the right expression if i am wrong thank you.

    Raj,
    i tested it with the emp table the following way:
    Extend This Application
    A page or application component
    Component > Form > Table
    [selected EMP table, page 40, the rest default]
    Edit Page 40
    Edit Page Item P40_EMPNO
    Alternate source used: Only when current value is session state is null
    Source Type: SQL Query
    Source value or expression: SELECT CT_ID_SEQ.NEXTVAL FROM DUAL;
    Apply Changes
    Run Page
    [The empno field shows a value of 50]
    [I enter 'test' into the ename field and click 'Create']
    [I receive the error: ORA-01400: cannot insert NULL into ("EAS"."EMP"."EMPNO")]
    I'm not sure what i am doing wrong here.
    Thanks

Maybe you are looking for