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());
    }
}
Balaji
  • 191
  • 3
  • 14
  • I see cobDate is a String but you have added @Temporal to a String field. Can you change the cobDate to Date and try? – lsiva Jul 26 '16 at 13:55
  • @Isiva my database column is of date datatype.Updating the question. I cannot make it a string and I am supposed to store it as date – Balaji Jul 26 '16 at 14:01
  • I was asking if you could change CcarRepWfInstDTO -> cobDate field to Date. The reason I was aking is by documentauion @Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. – lsiva Jul 26 '16 at 14:41
  • @lsiva Earlier I was doing that only. I was parsing the incomoing json using jackson mapper and it was completely breaking my code. Please find a post below which explains the issue I was facing and hence I reverted back to normal string http://stackoverflow.com/questions/38564154/store-date-from-ui-to-database-using-restful-webservice?noredirect=1#comment64523155_38564154 – Balaji Jul 26 '16 at 14:43
  • @lsiva Hope this clarifies and explains. – Balaji Jul 26 '16 at 14:45

1 Answers1

1

The mapping logic missed the date format. This can be defined in mapstruct like this:

@Mapping(target = "cobDate", source = "cobDate", dateFormat = "DD-MM-yyyy")
CcarReportWorkflowInstance convert(CcarRepWfInstDTO ccarRepWfInstDTO);
thunderhook
  • 497
  • 5
  • 21