private boolean assignPrimaryKeyInternal(Object entity, DatabaseEntry key)
      throws DatabaseException, RefreshException {

    /*
     * The keyFieldFormat is the format of a simple integer field.  For a
     * composite key class it is the contained integer field.  By writing
     * the Long sequence value using that format, the output data can then
     * be read to construct the actual key instance, whether it is a simple
     * or composite key class, and assign it to the primary key field in
     * the entity object.
     */
    if (entityFormat.isPriKeyNullOrZero(entity, rawAccess)) {
      Long value = sequence.get(null, 1);
      RecordOutput output = new RecordOutput(catalog, rawAccess);
      keyFieldFormat.writeObject(value, output, rawAccess);
      TupleBase.outputToEntry(output, key);
      EntityInput input =
          new RecordInput(
              catalog, rawAccess, null, 0, key.getData(), key.getOffset(), key.getSize());
      entityFormat.getReader().readPriKey(entity, input, rawAccess);
      return true;
    } else {
      return false;
    }
  }
Пример #2
0
  public void run(File envHomeDirectory) throws DatabaseException, IOException {

    /* Create the environment object. */
    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    Environment env = new Environment(envHomeDirectory, envConfig);

    /* Create the database object. */
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setAllowCreate(true);
    Database db = env.openDatabase(null, DB_NAME, dbConfig);

    /* Create the sequence oject. */
    SequenceConfig config = new SequenceConfig();
    config.setAllowCreate(true);
    DatabaseEntry key = new DatabaseEntry(KEY_NAME.getBytes("UTF-8"));
    Sequence seq = db.openSequence(null, key, config);

    /* Allocate a few sequence numbers. */
    for (int i = 0; i < 10; i++) {
      long seqnum = seq.get(null, 1);
      System.out.println("Got sequence number: " + seqnum);
    }

    /* Close all. */
    seq.close();
    db.close();
    env.close();
  }