1

If I'm trying to insert some record in my table which has a typical ID (integer not null) field, and I'm leaving the ID field empty (null), I have to make sure that correct value is used automatically.

What is a typical commonly used approach to this for EBean? (use a separate call to some database sequence, use a default ID value from a call to a sequence, ...)

P.S.: The database is PostgreSQL. So, I don't have this nice friendly MySQL feature of "auto_increment".

Sivakumar
  • 1,711
  • 14
  • 18
user3791111
  • 1,469
  • 1
  • 15
  • 20

2 Answers2

2

I think you need to set serial as datatype for primary key column in PostgresSQL for auto increment. Have a look at this post to know how serial works. So i didn't think any special change needed from ebean side.

Community
  • 1
  • 1
Sivakumar
  • 1,711
  • 14
  • 18
0

I just use @Id and do not set id and when you save, it should insert a record with generated id value.

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "My_Table")
public class MyTable extends Model {

    @Id
    private Integer primaryId;
    private String someField;

    public MyTable(String a) {
       someField = a;
    }
}

MyTable m = new MyTable("test");
m.save();