2

I have the following DTO,Entity,Mapper classes that persist value in database.I am facing issues in one of those attributes which is of Date column. I input a string in "DD-MM-yyyy" format. However it fails in the mapper class saying its an unparseable date. Am not sure why there is no default date format in the compile time generated class

Database structure

enter image description here DTO

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;

}

Entity

@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;

Snippet from the Mapper Implementation class generated during compile time which is failing and throwing back error saying unparseable date

    @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;
}

Code in the service layer which uses mapper class and fails

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());
    }
}

Custom code in the service layer without using mapper which uses explicitly defined simpledate format which is working fine. Please advise as to how to fix this and use Spring boot way stack since my whole applications is designed around it. It wont be good If I do not use the default supplied mapper class and implementation.

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());
    }
}
4

1 に答える 1