I read several similar posts here on SO but nothing has helped so far. I am using Java EE 8, Open Liberty 20.0.0.6 and PostgreSQL 12.
I run my locally using Docker which initializes the schema with:
CREATE TABLE IF NOT EXISTS PUBLIC.tab_todo
(
col_id SERIAL PRIMARY KEY,
col_name VARCHAR(80) NOT NULL,
col_desc VARCHAR(500),
col_due TIMESTAMP NOT NULL,
col_state BOOLEAN NOT NULL
);
My addTodo
method which persists a new element is pretty simple:
public Long addTodo(final Todo todo) {
LOG.info("Add todo");
entityManager.persist(todo);
LOG.info("Created todo: {}", todo);
return todo.getId();
}
But the id is always null
.
This is my Entity:
@Entity
@Table(name = "tab_todo")
public class Todo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
//@Column(name = "col_id", nullable = false) // This did not have any effect either
private Long id;
@Column(name = "col_name")
private String name;
@Column(name = "col_desc")
private String description;
@Column(name = "col_state")
private boolean status;
@Column(name = "col_due")
private LocalDateTime dueDate;
// Getter, Setter, etc.
Saving a new to do works fine. When I check the database then I can see that col_id
has been set properly. But JPA won't set the id in my entity when persisting.
What am I doing wrong here?