简体   繁体   中英

Cannot construct instance of `java.time.LocalDate` (no Creators, like default construct, exist): cannot deserialize from Object value

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)

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM