Class - Add a new attribute into a class

Sometimes we wonder how to add a property into a class. For example the property started into LongRunningTask.

1) First of all we add this property into class where we need it. Let's use our example:

@Column(name = "task_started")
private DateTime taskStarted;

Column annotation refers to database table column name (we do not have that column yet, but we will make it in third step). Also we need to add setters and getters.

public void setTaskStarted(DateTime date){
	taskStarted = date;
}
 
public DateTime getTaskStarted(){
	return taskStarted;
}

2) Secondly we need to adjust Dto class as well. We do not need column annotation anymore:

private DateTime taskStarted;
 
public void setTaskStarted(DateTime date){
	taskStarted = date;
}
 
public DateTime getTaskStarted(){
	return taskStarted;
}

3) Before building our backend we need to adjust our table in database. We are using flyway script for adding some column into already existing table. We need to be careful with name, because its beginning number have to follow rules created before. So for our script we are using name:

V1_00_029__long-running-task-task-strated.sql

following

V1_00_028__eav-change-confidential-storage-key.sql

For our example:

ALTER TABLE idm_long_running_task ADD COLUMN task_started TIMESTAMP WITHOUT TIME zone;
It is necessary to follow these steps before building backend

In the end we only build backend and can start using our property.

  • by poulm