Search code examples
javaoracle-databaseebean

Unable to create record with GENERATED IDENTITY primary key using Ebean


I have a table in Oracle Database 12c, created with the following DDL:

create table customer (
  id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name VARCHAR(100)
);

I am trying to insert a row to this table using Ebean.save method with the model defined below:

@Entity
@Table(name = "customer")
public class Customer {

  @Id
  Integer id;
  ....

The code used to insert the row is as follows:

Customer customer = new Customer();
customer.setName(name);
Ebean.save(customer);

It is failing with the following stack trace:

Caused by: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist

  at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:494)
  at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:446)
  at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1054)
  at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:623)
  ....

Caused by: Error : 2289, Position : 7, Sql = select 
  customer_seq.nextval, a from (select level as a FROM dual CONNECT BY level <= 20), OriginalSql = select customer_seq.nextval, a from (select level as a FROM dual CONNECT BY level <= 20), Error Msg = ORA-02289: sequence does not exist

From the error, what I understand is that Ebean is trying to use Sequence strategy for the identity column and it is failing. I've tried setting Identity strategy as follows without any success, maybe because Ebean is using Sequence strategy for Oracle DB as explained here.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;

Is there a way to save a row in a Oracle 12c table with autogenerated identity column as defined above with Ebean? Was I on the right track by setting the IDENTITY strategy?


Solution

  • This was a bug in Ebean, and is fixed in 11.10.1 release. The bug was reported on github following my post on Ebean's google group. I have verified that I can specify autogenerated identity column with this version of Ebean as follows:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Integer id;