Skip to main content

Key Generation

Oracle supports IDENTITY columns and has long used sequences for primary key generation. The recommended configurations in dbVisitor differ between these two approaches:

  • IDENTITY: generated by the database after insert, backfilled via JDBC generated keys.
  • sequence: get the sequence value before INSERT, then pass the primary key value into the INSERT.
Note

For general configuration, see Generated Keys.

Key Strategies

StrategyUsage on this datasource
AssignedSupply the primary key before insertion.
Auto-incrementUse a database-generated numeric key.
UUIDUse UUID32 or UUID36 with a string key field.
SequenceFetch a sequence value before insertion.
CustomUse a custom generator before or after insertion.

Using IDENTITY

Oracle 12c+ can use IDENTITY columns:

CREATE TABLE user_info (
id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR2(64),
age NUMBER
)

Configure KeyType.Auto on the primary-key property for automatic backfill after insertion.

Oracle IDENTITY mapping
@Table("user_info")
public class UserInfo {
@Column(value = "id", primary = true, keyType = KeyType.Auto)
private Integer id;

@Column("name")
private String name;

@Column("age")
private Integer age;
}
Automatic property backfill after insert
UserInfo user = new UserInfo();
user.setName("mali");
user.setAge(18);

LambdaTemplate lambda = ...;
int rows = lambda.insert(UserInfo.class)
.applyEntity(user)
.executeSumResult();

Integer id = user.getId();
Note
  • Multiple objects are inserted individually, with keys backfilled separately. JDBC batching is not used.
  • When an insert conflict skips the insert or updates an existing row, key backfill is not guaranteed.

Using a Sequence

This example uses a regular primary-key column and a sequence, independently of the IDENTITY example above:

CREATE TABLE user_info (
id NUMBER(10) PRIMARY KEY,
name VARCHAR2(100),
age NUMBER(3)
);
CREATE SEQUENCE user_info_seq START WITH 1 INCREMENT BY 1;

Configure the primary-key property with KeyType.Sequence and name the sequence with @KeySeq:

import net.hasor.dbvisitor.mapping.Column;
import net.hasor.dbvisitor.mapping.KeySeq;
import net.hasor.dbvisitor.mapping.KeyType;
import net.hasor.dbvisitor.mapping.Table;

@Table("user_info")
public class UserInfo {
@Column(primary = true, keyType = KeyType.Sequence)
@KeySeq("user_info_seq")
private Integer id;
private String name;
private Integer age;

// Getters and setters omitted
}
UserInfo user = new UserInfo();
user.setName("mali");

lambda.insert(UserInfo.class)
.applyEntity(user)
.executeSumResult();

Before each insert, dbVisitor runs SELECT user_info_seq.NEXTVAL FROM dual, assigns the value to user.id, and inserts the row. When inserting multiple entities, each receives its own sequence value.

Choosing the key source

For method annotations and Mapper files, use useGeneratedKeys with the default key source. The generated-key result is separate from the statement's ordinary result set; do not set generatedKeySource="resultSet" for these inserts.

For values returned by RETURNING ... INTO, use OUT parameters instead; see Returning data.