1

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":"-"}}]
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • looking at your json, seems like your `project` object is itself null, so you cannot see the values. Also creating empty object of `project` will not make sense, still finds your question incomplete, eidt and post the complete question with the expected output, problem you are facing. – Ankur Singhal Dec 18 '14 at 05:17
  • @ankur-singhal Yes, your assumption is correct, project is null because attributes of project such as `projectId` and `projectName` are null. So my question is how work around this issue? I have try to use `COALESCE` but it gives me error. See this post http://stackoverflow.com/questions/27528976/nvl-or-coalesce-in-jpa-namedquery – Jacob Dec 18 '14 at 05:22
  • @ankur-singhal I have included expected output in my question. – Jacob Dec 18 '14 at 05:32
  • Why do you want to handle this behavior in the entity layer? Have you considered implementing this behavior in a Service layer using Model objects rather than using the entities. The service layer could handle creating the 'default' Project model(s) if that's what is required. – MarkOfHall Dec 18 '14 at 05:49
  • @Polppan also we can do one thing, let entity save what it is, create an intermediate layer, when load from DB, create and call your service/utility, convert entity data to `POJO`, when `projects` is null, create an empty object, and set default values, and serialize these `POJO` – Ankur Singhal Dec 18 '14 at 05:57

0 Answers0