I am trying to set the time variable while the Date and Time variable is getting initialized by spring JPA but I am getting null in the time string. How should I approach this problem ?
@Entity
@Table(name = "cap_ratio")
public class CapacitiveInfoModel {
@Id
@Column(name = "time")
private Date dateAndTime;
@Transient
private String time;
public Date getDateAndTime() {
return dateAndTime;
}
public void setDateAndTime(Date dateAndTime) {
this.dateAndTime = dateAndTime;
this.time = DateAndTimeUtil.getFormattedDateAndTimeString(dateAndTime, "HH:mm");
}
public String getTime() {
return time;
}
}
CodePudding user response:
Firstly, When you are using the @Transient
annotation it means that the field will not persist in database since you are using jpa it is preferred to import this package javax.persistence.Transient
.
Further instead of taking time as String data type you can import the Date of util package and provide the Date data type for time and also provide annotation as @Temporal(TemporalType.TIME)
. I hope this should work. Also I would like to suggest you to give relevant column name as you have given time column name for dateAndTime field.
CodePudding user response:
You may use 'transient' keyword instead of 'transient' annotation. Also you can use @CreationTimestamp annotation for inserting time at an instant when it's value getting saved in database. For more info you can even read springs documentation by referring to the provided link: https://www.baeldung.com/jpa-transient-ignore-field
Hopefully it'll work. Thanks