【问题标题】:IntelliJIDEA show a type mismatch error with JPA (hibernate H2 dialect) but the code compileIntelliJIDEA 使用 JPA(休眠 H2 方言)显示类型不匹配错误,但代码编译
【发布时间】:2021-08-11 22:24:27
【问题描述】:

这是我的 IDE (IntelliJ IDEA) 向我展示的:

但是,当我编译时,我似乎没有任何错误! starttime 变量是一个 OffsetDateTime 变量。

为什么在我的 IDE 中显示错误?

更新

我还尝试重新启动 IDE 并使缓存无效,但没有任何反应。

这是我的 pom.xml 文件,其中包含所有依赖项:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<packaging>jar</packaging>
<name>...</name>
<version>1.0.0</version>
<properties>
    <java.version>11</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <springfox-version>2.8.0</springfox-version>
    <rest-assured.version>2.9.0</rest-assured.version>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
</parent>
<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--SpringFox dependencies -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox-version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>
    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.4.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.4.2.Final</version>
    </dependency>
    <!-- Persistence -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <!-- Bean Validation API support -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.11</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.2.11</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.11</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- Tests -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>${rest-assured.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

【问题讨论】:

  • 您能添加您的项目配置(依赖项/结构...)吗?
  • 完成!谢谢。
  • 看起来您正在使用 ?2?3 作为动态 SQL 参数。请尝试在设置中添加与?[number] 格式匹配的正则表达式模式(macOS 上的首选项)|工具 |数据库 |用户参数 -> 参数模式。有帮助吗?
  • 我用的是windows但是路径是一样的。我添加了"?[number[",但没有任何变化!
  • 你在字符串中注入了什么方言?我没有收到此错误。应用程序是否正常工作?如果问题仍然存在,请提供示例项目。

标签: java jpa intellij-idea h2 java-11


【解决方案1】:

CAST 是一个允许将数据库值转换为特定数据库类型的函数:

使用 CAST 将值转换为特定的数据库类型。

用法

CAST 函数独立于数据库,但需要数据库 支持。

处理此类查询的最简洁且无错误的方法是提供一种替代方法,将分别与存储值匹配的类型作为参数,在您的情况下为OffsetDateTime,并结合取决于您的业务逻辑的转换模式:

@Query("SELECT t FROM Transaction t WHERE t.userId = ?1 
    AND t.startTime BETWEEN ?2 AND ?3")
List<Transaction> getUserTransactionsBetweenTwoDates(final Long userId, 
    final OffsetDateTime begin, final OffsetDateTime end);

否则,您仍然可以将生成的 OffsetDateTime 值(即 TIMESTAMP)转换为 sql 类型 DATE

@Query("SELECT t FROM Transaction t WHERE t.userId = ?1 AND (CAST(t.startTime AS DATE)) BETWEEN ?2 AND ?3")
List<Transaction> getUserTransactionsBetweenTwoDates(final Long userId, final LocalDate begin, final LocalDate end);

【讨论】:

  • 您的第一个选项不起作用。它不会改变任何东西,确实我有以下编译错误:Type mismatch: number, date or string is expected。这是因为 OffsetDateTime 未被识别为数据库类型。第二个选项也不起作用,因为我在帖子中描述了相同的编译错误。
  • 为什么在存储数据时支持OffsetDateTime而不是在查询数据时?对于第二个提示,您仍然可以发布错误的整个堆栈跟踪,因为我看不到有效的查询是如何工作的。
  • 对不起@tmarwen,如果简单地使用我在帖子中写的第一个 sn-p 代码,我似乎没有任何编译错误。但是我的 IDE (IntellijIDEA) 继续显示类型不匹配错误。不知道转换是不是hibernate自动完成的。
  • 如果错误发生在 IDE 本身,最好添加带有突出显示错误的快照。 (您原来的帖子表明错误恰好发生在运行时)
猜你喜欢
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 2021-02-05
  • 2019-01-27
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多