Skip Headers

# Configuring Query Sequencing

Previous

Next

You can configure the query that TopLink uses to update or read a sequence value for any sequence type that extends `QuerySequence`.

In most applications, the queries that TopLink automatically uses are sufficient. However, if your application has special sequencing needs–for example, if you want to use stored procedures for sequencing–then you can configure the update and read queries that the TopLink sequence uses.

illustrates how to specify a stored procedure that updates a sequence and returns the new sequence value with a single SQL select query. In this example, the stored procedure is named `UPDATE_SEQ`. It contains one input argument–the name of the sequence to update (`SEQ_NAME`), and one output argument–the value of the sequence after the updated (`SEQ_COUNT`). The stored procedure increments the sequence value associated with the sequence named `SEQ_NAME` and returns the new sequence value in the output argument named `SEQ_COUNT`.

Using a Stored Procedure for both Sequence Update and Select

    ValueReadQuery seqReadQuery = new ValueReadQuery();
    StoredProcedureCall spCall = new StoredProcedureCall();
    spCall.setProcedureName("UPDATE_SEQ");
    seqReadQuery.addNamedArgument("SEQ_NAME");
    seqReadQuery.addNamedOutputArgument("SEQ_COUNT");
    seqReadQuery.setCall(spCall);
    ((QuerySequence)login.getDefaultSequence()).setSelectQuery(seqReadQuery);

and illustrate how to specify separate stored procedures for sequence update and select actions.

In , the stored procedure is named UPDATE_SEQ and it contains one input argument: the name of the sequence to update (`SEQ_NAME`). The stored procedure increments the sequence value associated with the sequence named `SEQ_NAME`.

Using a Stored Procedure for Sequence Updates Only

    DataModifyQuery seqUpdateQuery = new DataModifyQuery();
    StoredProcedureCall spCall = new StoredProcedureCall();
    spCall.setProcedureName("UPDATE_SEQ");
    seqUpdateQuery.addNamedArgument("SEQ_NAME");
    seqUpdateQuery.setCall(spCall);
    ((QuerySequence)login.getDefaultSequence()).setUpdateQuery(seqUpdateQuery);

In , the stored procedure is named `SELECT_SEQ` and it takes one argument: the name of the sequence to select from (`SEQ_NAME`). The stored procedure reads one data value: the current sequence value associated with the sequence name `SEQ_NAME`.

Using a Stored Procedure for Sequence Selects Only

    ValueReadQuery seqReadQuery = new ValueReadQuery();
    StoredProcedureCall spCall = new StoredProcedureCall();
    spCall.setProcedureName("SELECT_SEQ");
    seqReadQuery.addArgument("SEQ_NAME");
    seqReadQuery.setCall(spCall);
    login.((QuerySequence)getDefaultSequence()).setSelectQuery(seqReadQuery)

You can also create a `QuerySequence` directly and add it to your login, as Example: Using the QuerySequence Class shows.

Using the QuerySequence Class

    // Use the two-argument constructor: pass in sequence name and preallocation size.
    // Alternatively, you can use zero- or one-argument (sequence name) constructor
    login.addSequence(new QuerySequence("SEQ1", 75));