S2JDBC で INSERT 後に Oracle のシーケンス値 CURRVAL を取得するには

May 13, 2009

メモ。

エンティティ

@Entity
@Generated(value = {“S2JDBC-Gen 2.4.35”, “org.seasar.extension.jdbc.gen.internal.model.EntityModelFactoryImpl”}, date = “2009/05/11 10:51:08”)
public class ExampleEntity extends AbstractEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator=”EXAMPLE_ID_SEQUENCE_GEN”)
@SequenceGenerator(name=”EXAMPLE_ID_SEQUENCE_GEN”, sequenceName=”EXAMPLE_ID_SEQUENCE”, allocationSize = 1, initialValue = 1)
@Column(length = 3, nullable = false, unique = true)
public long id;

処理

newId = … のところで直前の新IDの値を取得している。

public class ExampleService extends AbstractService {
public void insertEntity(ExampleEntity exampleEntity) {
jdbcManager.insert(exampleEntity).execute();
long newId = jdbcManager.selectBySql(Long.class,
“select EXAMPLE_ID_SEQUENCE.currval from dual”).getSingleResult();

NEXTVAL であれば、DBMS方言用に Implementor にメソッドが用意されていたが、CURRVAL はなかった。

tilfin freelance software engineer