0

I am using Java 11 and Spring boot v2.3.3.RELEASE. Below is my pom.xml. I am trying to fetch the data from DB function, which will return 2 columns and one row. The columns returned are Date (Postgres SQL).

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.group</groupId>
    <artifactId>artifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

I have the following model class.

import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDate;


public class CachedDate {
    @JsonFormat(
            shape = JsonFormat.Shape.STRING,
            pattern = "yyyy-MM-dd",
            locale = "en_CA")
    private LocalDate businessDate;
    @JsonFormat(
            shape = JsonFormat.Shape.STRING,
            pattern = "yyyy-MM-dd",
            locale = "en_CA")
    private LocalDate previousBusinessDate;
// constructor/ getter and setter ommited
}

I am having a repository where I am trying to get the data based on DB function call.

Query query = entityManager.createNativeQuery(
        "SELECT * FROM get_function(:date)"
);
query.setParameter("date", date);
List<Object> result = (List<Object>) query.getResultList();
Iterator it = result.iterator();
CachedDate dates = new CachedDate();

while (it.hasNext()) {
    Object[] row = (Object[]) it.next();
    //dates.setBusinessDate((LocalDate) row[0]);
    //dates.setPreviousBusinessDate((LocalDate) row[1]);
     // This gives error 1


    dates.setBusinessDate(LocalDate.parse((String) row[0]));
    dates.setPreviousBusinessDate(LocalDate.parse((String) row[1]));

    // This gives error 2
}

Error 1:

Caused by: java.lang.ClassCastException: class java.sql.Date cannot be cast to class java.time.LocalDate (java.sql.Date is in module java.sql of loader 'platform'; java.time.LocalDate is in module java.base of loader 'bootstrap')

Error 2:

Caused by: java.lang.ClassCastException: class java.sql.Date cannot be cast to class java.lang.String (java.sql.Date is in module java.sql of loader 'platform'; java.lang.String is in module java.base of loader 'bootstrap')

What is the issue here? I cannot have the model class as Entity as it is not a object in my DB. It is just what is returned from function. Not sure if that would jave helped though.

Can you please assist here?

1
  • The query restuns a java.sql.Date not a String not a LocalDate. You cannot cast it. You should cast it to a java.sql.Date and convert that to a LocalDate. Commented Nov 3, 2020 at 6:48

1 Answer 1

2

The return type of your query is java.sql.Date. You have to cast it first to java.sql.Date then call .toLocalDate() to convert it to LocalDate.

Sample:

   dates.setBusinessDate(((java.sql.Date) row[0]).toLocalDate());
   dates.setPreviousBusinessDate(((java.sql.Date) row[1]).toLocalDate());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked. My bad. Earlier when I was using Java.util.date, I was directly casting it (string returned) to Date i.e. (Date) row[0] was working, so I just replaced it with LocalDate (when I replaced Java.util.date to Java.time.* . Thanks this helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.