如何解决将java.sql.Date转换为java.time.LocalDate时出现Java Spring Boot Jpa错误
我正在使用Java 11和Spring Boot v2.3.3.RELEASE。以下是我的pom.xml。 我正在尝试从DB函数获取数据,该函数将返回2列和1行。返回的列是日期(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>
我有以下模型课程。
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,locale = "en_CA")
private LocalDate prevIoUsBusinessDate;
// constructor/ getter and setter ommited
}
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
}
错误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')
错误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')
这里是什么问题? 我不能将模型类作为Entity,因为它不是我的数据库中的对象。这就是从函数返回的内容。不确定是否会有所帮助。
能在这里帮忙吗?
解决方法
查询的返回类型为java.sql.Date
。您必须先将其转换为java.sql.Date
,然后调用.toLocalDate()
将其转换为LocalDate
。
示例:
dates.setBusinessDate(((java.sql.Date) row[0]).toLocalDate());
dates.setPreviousBusinessDate(((java.sql.Date) row[1]).toLocalDate());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。