从我的
JpaRepository
方法中,我得到 null
作为 java.time.LocalDateTime
实体类中定义的 jpa
的值。
实体类
@Entity
@Table(name="otp_detail")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class OtpDetail implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String msisdn;
private String otp;
@Column(name = "otp_validity", nullable = false,columnDefinition = "TIMESTAMP")
private java.time.LocalDateTime otpValidity;
private String mpin;
private String isActive;
private String token;
public OtpDetail() {
}
public OtpDetail(String msisdn, String otp, java.time.LocalDateTime otpValidity, String isActive) {
this.msisdn = msisdn;
this.otp = otp;
this.otpValidity = otpValidity;
this.isActive = isActive;
}
public OtpDetail(String msisdn, String otp, java.time.LocalDateTime otpValidity, String mpin, String isActive) {
this.msisdn = msisdn;
this.otp = otp;
this.otpValidity = otpValidity;
this.mpin = mpin;
this.isActive = isActive;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "msisdn", unique = true, nullable = false, length = 45)
public String getMsisdn() {
return this.msisdn;
}
public void setMsisdn(String msisdn) {
this.msisdn = msisdn;
}
@Column(name = "otp", nullable = false, length = 45)
public String getOtp() {
return this.otp;
}
public void setOtp(String otp) {
this.otp = otp;
}
//@Temporal(TemporalType.TIMESTAMP)
//@JsonFormat(pattern = "dd-MM-yyyy HH:mm:ss")
//@Column(name = "otp_validity", nullable = false,columnDefinition = "TIMESTAMP")
public java.time.LocalDateTime getOtpValidity() {
return this.otpValidity;
}
public void setOtpValidity(java.time.LocalDateTime otpValidity) {
this.otpValidity = otpValidity;
}
@Column(name = "mpin", length = 10)
public String getMpin() {
return this.mpin;
}
public void setMpin(String mpin) {
this.mpin = mpin;
}
@Column(name = "isActive", nullable = false, length = 45)
public String getIsActive() {
return this.isActive;
}
public void setIsActive(String isActive) {
this.isActive = isActive;
}
@Column(name = "token")
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
@Override
public String toString() {
return "OtpDetail [id=" + id + ", msisdn=" + msisdn + ", otp=" + otp + ", otpValidity=" + otpValidity
+ ", mpin=" + mpin + ", isActive=" + isActive + ", token=" + token + "]";
}
}
存储库
public interface OtpDetailRepository extends JpaRepository<OtpDetail, Integer>{
OtpDetail findByMsisdn(String msisdn);
}
控制器
@Autowired
OtpDetailRepository otpRepo;
@GetMapping(value="test")
public OtpDetail test(){
OtpDetail otpObj= otpRepo.findByMsisdn("90909091");
return otpObj;
}
回复
{
"id": 3,
"msisdn": "90909091",
"otp": "5130",
"otpValidity": null, //Why am I getting null here?
"mpin": "1234",
"isActive": "true",
"token": "943225746"
}
我在
mysql
中确实有 otp_validity 作为“2023-11-22 09:43:47”,但我得到的响应为空。早些时候我曾经使用java.util.Date
。因为它已被弃用,现在想要使用 java.time.LocalDateTime
,它最好返回时间戳,即日期+时间。但它返回 null。我哪里错了?
@Column(name = "otp_validity", columnDefinition = "TIMESTAMP")
private LocalDateTime localDateTime() {
return this.otpValidity;
}