2

Please, help me with the next problem: I'm calling a mysql SP with simpleJdbcCall, but i have a problem with LocalDate attribute in my bean. I hope someone can help me

SP

CREATE DEFINER=`dbadmin`@`%` PROCEDURE `SP_LIST_EQUIPMENT`()
BEGIN
    SELECT `ID`,
        `PO_NUMBER`,
        `BRAND`,
        `SKU`,
        `DESCRIPTION`,
        `IMEI`,
        `QUANTITY`,
        `DISPATCH_DATE`,
        `CREATED_BY`,
        `UPDATED_BY`
    FROM `mandato_db`.`mandato_equipment`;
END

Bean

public class Equipment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String poNumber;
    private String brand;
    private String sku;
    private String description;
    private String imei;
    private int quantity;
    private LocalDate dispatchDate;
    private String createdBy;
    private String updatedBy;

}

Dao

    @Override
    public List<Equipment> list() {
        String procedureName = "SP_LIST_EQUIPMENT";
        simpleJdbcCall = new SimpleJdbcCall(dataSource).withProcedureName(procedureName)
                .returningResultSet("equipments", BeanPropertyRowMapper.newInstance(Equipment.class));
        Map<String, Object> out = simpleJdbcCall.execute();

        ObjectMapper mapper = new ObjectMapper();
        // Here the error occurs
        List<Equipment> list = mapper.convertValue(out.get("equipments"), new TypeReference<List<Equipment>>() {});

        return list;
    }

StackTrace

org.springframework.dao.InvalidDataAccessApiUsageException: Cannot construct instance of java.time.LocalDate (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: java.util.ArrayList[0]->pe.com.ripley.mandatobackend.bean.Equipment["dispatchDate"]); nested exception is java.lang.IllegalArgumentException: Cannot construct instance of java.time.LocalDate (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: java.util.ArrayList[0]->pe.com.ripley.mandatobackend.bean.Equipment["dispatchDate"]) at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:374)

Hadi J
  • 16,989
  • 4
  • 36
  • 62

1 Answers1

0

Java 8 allow you to map the JDBC Types TIME, DATE, and TIMESTAMP to the java.time types – LocalTime, LocalDate, and LocalDateTime.

To make the conversion between the SQL Date and LocalDate you will have to use the annotation before the declaration on dispatchDate in your class Equipment

 @Column(name = "local_date", columnDefinition = "DATE")
 private LocalDate dispatchDate;
  • Not quite correct. The legacy `java.sql.Timestamp` class was replaced by `java.time.OffsetDateTime` or `Instant`. JDBC 4.2 requires support for `OffsetDateTime`. These all represent a moment, a specific point on the timeline. `LocalDateTime` does *not* represent a moment. – Basil Bourque Dec 27 '19 at 21:05