Identity Column Generated by Database
Sormula uses Statement.html#getGeneratedKeys
to get database generated key values for columns declared as IDENTITY and with corresponding
field annotated as @Column(identity=true).
Student student = new Student(); // id initialized to zero
student.setFirstName("Jeff");
student.setLastName("Miller");
student.setGraduationDate(new Date(System.currentTimeMillis()));
table.insert(student); // sormula invokes student.setId() with next generated id
System.out.println("row inserted with id=" + student.getId());
Output
row inserted with id=1001
Student DDL
CREATE TABLE student
(
id INTEGER GENERATED ALWAYS AS IDENTITY(START WITH 1001) PRIMARY KEY,
firstname VARCHAR(30),
lastname VARCHAR(30),
graduationdate TIMESTAMP
)
Student.java
public class Student
{
@Column(identity=true)
int id;
String firstName;
String lastName;
LocalDate graduationDate;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
firstName, lastName, and graduationDate getters/setters...
}
See /src/examples/java/org/sormula/examples/identity/IdentityInsert.java in the project.
|