我有以下 DTO、Entity、Mapper 类将值保留在数据库中。我在 Date 列的其中一个属性中遇到问题。我以“DD-MM-yyyy”格式输入一个字符串。但是它在映射器类中失败,说它是一个不可解析的日期。不知道为什么编译时生成的类中没有默认日期格式
数据库结构
public class CcarRepWfInstDTO implements Serializable {
private Long id;
private Long reportId;
private Long workflowInstanceId;
private String cobDate;
private String frequency;
private String runType;
private String reportVersion;
private String workflowStatus;
}
实体
@Data
@Entity(name = "CcarReportWorkflowInstance")
@Table(name = "CCAR_REPORT_WORKFLOW_INSTANCE")
public class CcarReportWorkflowInstance implements Serializable {
private static final long serialVersionUID = 1L;
@SequenceGenerator(name = "CCAR_REPORT_WORKFLOW_INSTANCESEQ", sequenceName = "SEQ_CCAR_REPORT_WF_INST_ID", allocationSize = 1)
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CCAR_REPORT_WORKFLOW_INSTANCESEQ")
private Long id;
@Column(name = "WORKFLOW_INSTANCE_ID", nullable = false)
private Long workflowInstanceId;
@Column(name = "COB_DATE", nullable = false)
@Temporal(TemporalType.DATE)
private Date cobDate;
@Column(name = "FREQUENCY", nullable = false)
private String frequency;
@Column(name = "RUN_TYPE", nullable = false)
private String runType;
@Column(name = "REPORT_VERSION", nullable = false)
private String reportVersion;
@Column(name = "TERMINATION_STATUS", nullable = false)
private String terminationStatus;
@Version
@Column(name = "VERSION", columnDefinition = "integer DEFAULT 0", nullable = false)
private Long version = 0L;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REPORT_ID", nullable = false, insertable = false, updatable = false)
private CcarReport ccarReport;
@Column(name = "REPORT_ID")
private Long reportId;
在编译期间生成的 Mapper 实现类的代码段失败并返回错误说无法解析的日期
@Override
public CcarReportWorkflowInstance ccarRepWfInstDTOToCcarRepWfInst(CcarRepWfInstDTO ccarRepWfInstDTO) {
if ( ccarRepWfInstDTO == null ) {
return null;
}
CcarReportWorkflowInstance ccarReportWorkflowInstance = new CcarReportWorkflowInstance();
ccarReportWorkflowInstance.setId( ccarRepWfInstDTO.getId() );
ccarReportWorkflowInstance.setWorkflowInstanceId( ccarRepWfInstDTO.getWorkflowInstanceId() );
if ( ccarRepWfInstDTO.getCobDate() != null ) {
try {
ccarReportWorkflowInstance.setCobDate( new SimpleDateFormat().parse( ccarRepWfInstDTO.getCobDate() ) );
}
catch ( ParseException e ) {
throw new RuntimeException( e );
}
}
ccarReportWorkflowInstance.setFrequency( ccarRepWfInstDTO.getFrequency() );
ccarReportWorkflowInstance.setRunType( ccarRepWfInstDTO.getRunType() );
ccarReportWorkflowInstance.setReportVersion( ccarRepWfInstDTO.getReportVersion() );
ccarReportWorkflowInstance.setReportId( ccarRepWfInstDTO.getReportId() );
return ccarReportWorkflowInstance;
}
使用映射器类并失败的服务层中的代码
private void persistWfInst(CcarRepWfInstDTO ccarRepWfInstDTO) {
try {
ccarRepWfInstDTO.setWorkflowInstanceId(ccarRepWfInstDTO.getWorkflowInstanceId());
CcarReportWorkflowInstance ccarReportWorkflowInstance = ccarRepWfInstMapper.ccarRepWfInstDTOToCcarRepWfInst(ccarRepWfInstDTO);
ccarRepWfInstRepository.save(ccarReportWorkflowInstance);
} catch (Exception e) {
log.info("Exception while saving workflow instance information =" + e);
throw new CustomParameterizedException(e.getMessage());
}
}
服务层中的自定义代码不使用使用明确定义的 simpledate 格式的映射器,该格式工作正常。请告知如何解决此问题并使用 Spring 引导方式堆栈,因为我的整个应用程序都是围绕它设计的。如果我不使用默认提供的映射器类和实现,那就不好了。
private void persistWfInst(CcarRepWfInstDTO ccarRepWfInstDTO) {
try {
ccarRepWfInstDTO.setWorkflowInstanceId(ccarRepWfInstDTO.getWorkflowInstanceId());
CcarReportWorkflowInstance ccarReportWorkflowInstance = new CcarReportWorkflowInstance();
ccarReportWorkflowInstance.setId(ccarRepWfInstDTO.getId());
ccarReportWorkflowInstance.setWorkflowInstanceId(ccarRepWfInstDTO.getWorkflowInstanceId());
if (ccarRepWfInstDTO.getCobDate() != null) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("DD-MM-yyyy");
ccarReportWorkflowInstance.setCobDate(sdf.parse(ccarRepWfInstDTO.getCobDate()));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
ccarReportWorkflowInstance.setFrequency(ccarRepWfInstDTO.getFrequency());
ccarReportWorkflowInstance.setRunType(ccarRepWfInstDTO.getRunType());
ccarReportWorkflowInstance.setReportVersion(ccarRepWfInstDTO.getReportVersion());
ccarReportWorkflowInstance.setReportId(ccarRepWfInstDTO.getReportId());
ccarRepWfInstRepository.save(ccarReportWorkflowInstance);
} catch (Exception e) {
log.info("Exception while saving workflow instance information =" + e);
throw new CustomParameterizedException(e.getMessage());
}
}