有一个TaskVersion类,因为多加两个字段使用mybatis没法插入数据,请问怎么解决?
问题
我有一个TaskVersion类,这类的属性,对应数据库的每个字段
然后我想加两个字段
private String createTimeStr;
private String updateTimeStr;
createTimeStr
和updateTimeStr
分别是createTime和updateTimeStr转换成字符串的结果
查询的时候没问题,但是插入数据的时候,就是因为数据库没有这两个字段而报错,请问怎么解决?
package cn.spring.inter.entity;
import java.sql.Timestamp;
public class TaskVersion {
private Integer id;
private String version;//用户名
private Integer userID;//密码
private Integer taskID;//密码
private String code;
private Timestamp createTime;//密码
private Timestamp updateTime;
private String testResult;
private String remark;
private String status;
private Integer best;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Integer getUserID() {
return userID;
}
public void setUserID(Integer userID) {
this.userID = userID;
}
public Integer getTaskID() {
return taskID;
}
public void setTaskID(Integer taskID) {
this.taskID = taskID;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
public Timestamp getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Timestamp updateTime) {
this.updateTime = updateTime;
}
public String getTestResult() {
return testResult;
}
public void setTestResult(String testResult) {
this.testResult = testResult;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getBest() {
return best;
}
public void setBest(Integer best) {
this.best = best;
}
}
补充
我使用@Transient之后,
无法使用这段函数
this.updateDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.updateDate);
请问如何解决
package cn.spring.inter.entity;
import io.swagger.annotations.ApiModel;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
@Entity
public class TaskVersion {
@Id
@GeneratedValue private Integer id;
private String version;//用户名
private Integer userID;//密码
private Integer taskID;//密码
private String code;
private Timestamp createDate;
private Timestamp updateDate;
@Transient
private transient String name;
@Transient
private String createDateStr;
@Transient
private String updateDateStr;
private String testResult;
private String remark;
private String status;
private Integer best;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Integer getUserID() {
return userID;
}
public void setUserID(Integer userID) {
this.userID = userID;
}
public Integer getTaskID() {
return taskID;
}
public void setTaskID(Integer taskID) {
this.taskID = taskID;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Timestamp getCreateDate() {
return createDate;
}
public void setCreateDate(Timestamp createDate) {
this.createDate = createDate;
}
public Timestamp getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Timestamp updateDate) {
this.updateDate = updateDate;
}
public String getTestResult() {
return testResult;
}
public void setTestResult(String testResult) {
this.testResult = testResult;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getBest() {
return best;
}
public void setBest(Integer best) {
this.best = best;
}
@Transient
public String getCreateDateStr() {
// this.createDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.createDate);
return createDateStr;
}
public void setCreateDateStr(String createDateStr) {
this.createDateStr = createDateStr;
}
//
@Transient
public String getUpdateDateStr() {
// this.updateDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.updateDate);
return updateDateStr;
}
@Transient
public void setUpdateDateStr(String updateDateStr) {
this.updateDateStr = updateDateStr;
}
@Transient
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
插入数据的时候还是会报错,请问如何解决?
{
"timestamp": "2021-03-29T09:02:56.544+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: cn.spring.inter.entity.ResponseResult[\"data\"]->cn.spring.inter.entity.TaskVersion[\"updateDateStr\"])",
"trace": "org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: cn.spring.inter.entity.ResponseResult[\"data\"]->cn.spring.inter.entity.TaskVersion[\"updateDateStr\"])\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:296)\n\tat org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:289)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)\n\tat org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.lang.Thread.run(Thread.java:748)\nCaused by: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: cn.spring.inter.entity.ResponseResult[\"data\"]->cn.spring.inter.entity.TaskVersion[\"updateDateStr\"])\n\tat com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:394)\n\tat com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:353)\n\tat com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:316)\n\tat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:727)\n\tat com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)\n\tat com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)\n\tat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)\n\tat com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)\n\tat com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)\n\tat com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)\n\tat com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1396)\n\tat com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:913)\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:287)\n\t... 52 more\nCaused by: java.lang.NullPointerException\n\tat java.util.Calendar.setTime(Calendar.java:1770)\n\tat java.text.SimpleDateFormat.format(SimpleDateFormat.java:943)\n\tat java.text.SimpleDateFormat.format(SimpleDateFormat.java:936)\n\tat java.text.DateFormat.format(DateFormat.java:345)\n\tat cn.spring.inter.entity.TaskVersion.getUpdateDateStr(TaskVersion.java:136)\n\tat sun.reflect.GeneratedMethodAccessor103.invoke(Unknown Source)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:687)\n\tat com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)\n\t... 61 more\n",
"path": "/v1/task-version/"
}
最终解决办法
package cn.spring.inter.entity;
import io.swagger.annotations.ApiModel;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
//@Entity
@ApiModel("任务版本类")
public class TaskVersion {
// @Id
// @GeneratedValue
private Integer id;
private String version;//用户名
private Integer userID;//密码
private Integer taskID;//密码
private String code;
private Timestamp createDate;
private Timestamp updateDate;
@Transient
private transient String name;
@Transient
private String createDateStr;
@Transient
private String updateDateStr;
private String testResult;
private String remark;
private String status;
private int best;
@Transient
private String versionExplanation;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Integer getUserID() {
return userID;
}
public void setUserID(Integer userID) {
this.userID = userID;
}
public Integer getTaskID() {
return taskID;
}
public void setTaskID(Integer taskID) {
this.taskID = taskID;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Timestamp getCreateDate() {
return createDate;
}
public void setCreateDate(Timestamp createDate) {
this.createDate = createDate;
}
public Timestamp getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Timestamp updateDate) {
this.updateDate = updateDate;
}
public String getTestResult() {
return testResult;
}
public void setTestResult(String testResult) {
this.testResult = testResult;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getBest() {
return best;
}
public void setBest(int best) {
this.best = best;
}
@Transient
public String getVersionExplanation() {
return versionExplanation;
}
public void setVersionExplanation(String versionExplanation) {
this.versionExplanation = versionExplanation;
}
@Transient
public String getCreateDateStr() {
// System.out.println("this.createDate == " + this.createDate);
if(this.createDate != null) {
this.createDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.createDate);
}
return createDateStr;
}
public void setCreateDateStr(String createDateStr) {
this.createDateStr = createDateStr;
}
@Transient
public String getUpdateDateStr() {
// System.out.println("this.updateDate == " + this.updateDate);
if(this.updateDate != null) {
this.updateDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.updateDate);
}
return updateDateStr;
}
public void setUpdateDateStr(String updateDateStr) {
this.updateDateStr = updateDateStr;
}
@Transient
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
加个注解
@javax.persistence.Transient