I am trying to set default values for entity attributes so that if values are null in database, it would display default values.
My entity relationships are as follows
@Entity
@Table(name = "EMP")
public class Emp implements Serializable {
@Id
@Column(name = "empno", nullable = false)
private String empNo;
@JsonBackReference("proj")
@ManyToOne
@JoinColumn(name = "PROJECT_ID", referencedColumnName = "PROJECT_ID")
private Project project;
public Project getProject() {
return Project;
}
public void setProject(Project project) {
this.project = project;
// this.project = project == null ? <notSure> : project; // not sure what to give default value
}
@Entity
@Table(name = "PROJECTS")
public class Project implements Serializable {
@Id
@Column(name="PROJECT_ID", nullable = false)
private Long projectId = 0L; // default value
@Column(name = "PROJECT_NAME" , nullable = true)
private String projectName = "-"; // default value
@JsonManagedReference("proj")
@OneToMany(mappedBy = "project")
private List<Emp> empList;
public Long getProjectId() {
return projectId;
}
public void setProjectId(Long projectId) {
// this.projectId = projectId;
this.projectId = projectId == null ? 0L : projectId;
}
public String ProjectName() {
return projectName;
}
I am not sure what to give default value for this
this.project = project == null ? <notSure> : project;
Although default values are set for projectId
and projectName
, when I execute, I still see project attributes as null. What could be the reason for this and how could I set default values for project attributes?
Generated values
[{"empNo":"12690","empName":"SCOTT","project":null}]
There are values exist for empNo and empName, however for project.projectId and projectName, there are values exist in database table and hence I set the default value.
Expected output as per default values set for projectId
and projectName
[{"empNo":"12690","empName":"SCOTT","project":{"projectId":0,"projectName":"-"}}]