Aravind R
Aravind R

Reputation: 766

JPA how to set Modified Date and Completed Date NULL

I am new to JPA and I am trying to persist an Entity that will run through 3 stages New, Processing and Completed. I want the below timestamps to be updated accordingly

@Entity 
@Table(name = "feedertable")
@JsonIgnoreProperties(value = { "processStatus" })
 public class FeederTable {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private String id;
private String orderID;
private String status;

@Column(name="CREATEDDATETIME",nullable = false)
private Timestamp createdDateTime;

@Column(name="PROCESSSTATUS",nullable = false)
private String processStatus;

@Column(name="HOSTNAME",nullable = true)
private String hostName;

//@Temporal(TemporalType.TIMESTAMP)
@Column(name="PROCESSINGDATETIME",nullable = false)
private Timestamp processingDateTime;

@Column(name="COMPLETEDDATETIME",nullable = false)
private Timestamp completedDateTime; public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getOrderID() {
    return orderID;
}

public void setOrderID(String orderID) {
    this.orderID = orderID;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public Timestamp getCreatedDateTime() {
    return createdDateTime;
}

public void setCreatedDateTime(Timestamp createdDateTime) {
    this.createdDateTime = createdDateTime;
}

public String getProcessStatus() {
    return processStatus;
}

public void setProcessStatus(String processStatus) {
    this.processStatus = processStatus;
}

public String getHostName() {
    return hostName;
}

public void setHostName(String hostName) {
    this.hostName = hostName;
}

public Timestamp getProcessingDateTime() {
    return processingDateTime;
}

public void setProcessingDateTime(Timestamp processingDateTime) {
    this.processingDateTime = processingDateTime;
}

public Timestamp getCompletedDateTime() {
    return completedDateTime;
}

public void setCompletedDateTime(Timestamp completedDateTime) {
    this.completedDateTime = completedDateTime;
}

@Override
public String toString() {
    return "FeederTable{" +
            "id='" + id + '\'' +
            ", orderID='" + orderID + '\'' +
            ", status='" + status + '\'' +
            ", createdDateTime=" + createdDateTime +
            ", processStatus='" + processStatus + '\'' +
            ", hostName='" + hostName + '\'' +
            ", processingDateTime=" + processingDateTime +
            ", completedDateTime=" + completedDateTime +
            '}';
}

Persist in JPARepository:

FeederTable feederTable = new FeederTable(); 
feederTable.setOrderID(orderId);
 feederTable.setStatus(status);
 feederTable.setCreatedDateTime(new Timestamp(format.getTime())); feederTable.setProcessStatus("New");
 feederTable.setHostName(hostname); 
feederTable.setCompletedDateTime(null); 
feederTable.setProcessingDateTime(null);
 repository.save(feederTable); 

However when first time the Entity is persisted, All the Timestamps are populated. I want the PROCESSINGDATETIME and COMPLETEDDATETIME as null and populate it only when it reaches corresponding status. Any idea how to acheive it?.

Upvotes: 0

Views: 1937

Answers (1)

tryingToLearn
tryingToLearn

Reputation: 11659

I did a similar thing in my project using @PrePersist and @PreUpdate annotations.

There are other lifecycle hooks that you can use. Read about them here.

MyEntity{

    @Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;

    @Temporal(TemporalType.TIMESTAMP)
    private Date updationDate;

    @PrePersist
    public void onCreate() {
        creationDate = new Date();
    }

    @PreUpdate
    public void onUpdate() {
        updationDate = new Date();
    }
}

Did I understand your question correctly? Is this what you were looking for?

Upvotes: 1

Related Questions