簡體   English   中英

H2,Spring boot-JPA:使用 OffsetTime 字段檢索實體的問題

[英]H2, Spring boot-JPA: Issue with retrieving entity with OffsetTime field

我有一個帶有 H2 數據庫(版本 1.4.200)的小型 Spring Boot 應用程序(spring-data-jpa)。 我有 Shift 對象,代表一個重復的時間間隔:

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Shift {
    @Id
    @GeneratedValue
    private Long id;
    @Column(name = "start_time", columnDefinition = "TIME WITH TIME ZONE")
    private OffsetTime startTime;
    @Column(name = "end_time", columnDefinition = "TIME WITH TIME ZONE")
    private OffsetTime endTime;

    public Shift (OffsetTime startTime, OffsetTime endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
    }
}
@RestController
@RequiredArgsConstructor
public class Controller {

    private final ShiftRepository shiftRepository;
    @RequestMapping("/")
    public Shift index() {
        return shiftRepository.getById(1L);
    }

}
public interface ShiftRepository extends JpaRepository<Shift, Long> {
}
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
@Configuration
public class LoadDatabase {
    @Bean
    CommandLineRunner initUserTable(ShiftRepository repository) {
        return args -> repository
                .save(
                        new Shift(OffsetTime.parse("10:15:30+00:00"), OffsetTime.parse("11:25:30+00:00")));
    }}

檢索它會導致:

原因:org.h2.jdbc.JdbcSQLNonTransientException:一般錯誤:“java.lang.NullPointerException:無法調用”“org.h2.engine.CastDataProvider.currentTimestamp()”,因為“<parameter1>”為空”[ 50000-200] 在 org.h2.message.DbException.getJdbcSQLException(DbException.java:505) ~[h2-1.4.200.jar:1.4.200] 在 org.h2.message.DbException.getJdbcSQLException(DbException.java: 429)~[h2-1.4.200.jar:1.4.200] at org.h2.message.DbException.get(DbException.java:194)~[h2-1.4.200.jar:1.4.200] at org.h2.message.DbException.get(DbException.java:194) h2.message.DbException.convert(DbException.java:347) ~[h2-1.4.200.jar:1.4.200] at org.h2.message.DbException.toSQLException(DbException.java:319) ~[h2-1.4 .200.jar:1.4.200] 在 org.h2.message.TraceObject.logAndConvert(TraceObject.java:366) ~[h2-1.4.200.jar:1.4.200] 在 org.h2.jdbc.JdbcResultSet.getTime (JdbcResultSet.java:480) ~[h2-1.4.200.jar:1.4.200] at com.zaxxer.hikari.pool.HikariProxyResultSet.getTime(HikariProxyResultSet.java) ~[HikariCP-3.4.5.jar:na]在 org.hibernate.type.descriptor.sql.TimeT ypeDescriptor$2.doExtract(TimeTypeDescriptor.java:84) ~[hibernate-core-5.4.31.Final.jar:5.4.31.Final] at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java: 47) ~[hibernate-core-5.4.31.Final.jar:5.4.31.Final] 在 org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) ~[hibernate-core-5.4.31.Final .jar:5.4.31.Final] 在 org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253) ~[hibernate-core-5.4.31.Final.jar:5.4.31.Final] 在 org.hibernate .type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:243) ~[hibernate-core-5.4.31.Final.jar:5.4.31.Final] at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:329) ~[hibernate-core-5.4.31.Final.jar:5.4.31.Final] at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:3130) ~[hibernate-core-5.4.31.Final .jar:5.4.31.Final] 在 org.hibernate.loader.plan.exec.process.int ernal.EntityReferenceInitializerImpl.loadFromResultSet(EntityReferenceInitializerImpl.java:342) ~[hibernate-core-5.4.31.Final.jar:5.4.31.Final] ...省略了73個常用幀

在調試器中,我看到確實沒有設置時區: 在此處輸入圖像描述

我的 application.properties 可能相關:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

# Enabling H2 Console
spring.h2.console.enabled=true

# Custom H2 Console URL
spring.h2.console.path=/h2

pom.xml 中的相關行:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>15</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

我是否需要為 TIME WITH TIME ZONE 數據庫類型和 OffsetTime Java 類型使用一些自定義轉換器?

當我將類型更改為:

    @Column(name = "start_time", columnDefinition = "TIME")
    private OffsetTime startTime;
    @Column(name = "end_time", columnDefinition = "TIME")
    private OffsetTime endTime;

Evgenij Ryazanov 的上述評論很有幫助。 我遇到了同樣的問題,並且能夠通過降級 H2 的版本來解決它,所以我在這里將其提升為答案。

在您的pom.xml中,更新為:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <!-- There is a bug with 1.4.200 and retrieving dates back from the DB -->
        <version>1.4.199</version>
    </dependency>

Spring Boot在 2.1.10 版本中從 1.4.199 更改。 Spring Boot 2.7 使用了新的 2.x 版本的 H2。 大概這也是一個修復,但我還沒有嘗試過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM