RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1582589
Accepted
invzbl3
invzbl3
Asked:2024-06-03 23:15:25 +0000 UTC2024-06-03 23:15:25 +0000 UTC 2024-06-03 23:15:25 +0000 UTC

自动向H2数据库填充数据时,如何修复错误:“类型“BINARY(255)”和“CHARACTER VARYING(255)”的值不可比较”?

  • 772

在处理Java该项目时,使用以下技术:

<properties>
    <java.version>11</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <!-- Spring Boot Starter Web for building web, including RESTful, applications using Spring MVC -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Boot Starter Data JPA for Spring Data JPA and Hibernate -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <!--<version>2.7.18</version>-->
    </dependency>
    <!-- Spring Boot Starter Test for testing Spring Boot applications -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <!-- H2 Database for in-memory database (for testing purposes) -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>2.1.214</version> <!--Values of types "BINARY(255)" and "CHARACTER VARYING(255)" are not comparable-->
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>
    <!-- Jackson for JSON processing -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <!-- Spring Boot Maven Plugin for packaging Spring Boot applications -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <!-- Lombok Maven Plugin for delombok process -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.24</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

使用该文件时application.properties:

spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.sql.init.mode=always
spring.sql.init.schema-locations=classpath:schema.sql
spring.sql.init.data-locations=classpath:import.sql
server.port=8081
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql=TRACE
spring.h2.console.path=/h2-console

我收到运行时错误:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Values of types "BINARY(255)" and "CHARACTER VARYING(255)" are not comparable; SQL statement:
alter table backlog_item_tasks add constraint FKigou96u26bqhftbug6ba1akfa foreign key (tasks_id) references task [90110-214]

...

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Values of types "BINARY(255)" and "CHARACTER VARYING(255)" are not comparable; SQL statement:
alter table backlog_item_tasks add constraint FK1jlaqd1fh1t60yjn2mu179ra1 foreign key (backlog_item_id) references backlog_item [90110-214]

...

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Values of types "BINARY(255)" and "CHARACTER VARYING(255)" are not comparable; SQL statement:
alter table estimation_log_entry add constraint FK4qne2wpk2c906qwlb0qt17k7s foreign key (task_id) references task [90110-214]

我尝试过的:

h2我尝试使用预先准备的SQL脚本(例如 import.sql( ))向数据库填充所需的数据DML скрипт:

INSERT INTO backlog (id, name, description)
VALUES ('uuid-1', 'Backlog 1', 'Description for backlog 1');
INSERT INTO backlog_item (id, status, story, story_points, summary, type, product_id, release_id, sprint_id, backlog_id)
VALUES ('uuid-2', 'Open', 'Story 1', 5, 'Summary for story 1', 'Feature', 'uuid-product', 'uuid-release', 'uuid-sprint',
        'uuid-1');
INSERT INTO task (id, name, description, hours_remaining, volunteer, backlog_item_id)
VALUES ('uuid-3', 'Task 1', 'Description for task 1', 10, 'Volunteer 1', 'uuid-2');

相应地,schema.sql( DDL скрипт):

CREATE TABLE backlog
(
    id          VARCHAR(255) PRIMARY KEY,
    name        VARCHAR(255) NOT NULL,
    description TEXT
);

CREATE TABLE backlog_item
(
    id           VARCHAR(255) PRIMARY KEY,
    status       VARCHAR(255),
    story        TEXT,
    story_points INT,
    summary      TEXT,
    type         VARCHAR(255),
    product_id   VARCHAR(255),
    release_id   VARCHAR(255),
    sprint_id    VARCHAR(255),
    backlog_id   VARCHAR(255),
    FOREIGN KEY (backlog_id) REFERENCES backlog (id)
);

CREATE TABLE task
(
    id              VARCHAR(255) PRIMARY KEY,
    name            VARCHAR(255) NOT NULL,
    description     TEXT,
    hours_remaining INT,
    volunteer       VARCHAR(255),
    backlog_item_id VARCHAR(255),
    FOREIGN KEY (backlog_item_id) REFERENCES backlog_item (id)
);

配置application.properties如下:

1. Simple In-Memory Database
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

上述模式仅在使用的浏览器中显示http://localhost:8081/h2-console/。


和:

3. Persistent Database in File
spring.datasource.url=jdbc:h2:file:./data/mydb`

ошибки выполнения按以下格式播放中间件:

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLNonTransientConnectionException: Unsupported database file version or invalid file header in file "E:/IdeaProject/PDP1/data/mydb.mv.db" [90048-214]

...

Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Unsupported database file version or invalid file header in file "E:/IdeaProject/PDP1/data/mydb.mv.db"

...

Caused by: org.h2.mvstore.MVStoreException: The write format 3 is larger than the supported format 2 [2.1.214/5]

更新:

application.properties具有以下jdbc:h2:file选项:

spring.datasource.url=jdbc:h2:file:./data/mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

由于mydb.mv.db文件被锁定,也无法正常工作。

预期的:

预计在IDEA (Intellij IDEA)中收到以下格式的内容H2 бд:

在此输入图像描述

java
  • 1 1 个回答
  • 18 Views

1 个回答

  • Voted
  1. Best Answer
    invzbl3
    2024-06-03T23:15:25Z2024-06-03T23:15:25Z

    因此,为了解决该问题,需要执行以下操作:

    ““BINARY(255)”和“CHARACTER VARYING(255)”类型的值不具有可比性”

    H2在数据库中填写数据时:

    1. 将缺少的注释添加到simple data需要它的字段中:

    @Column(columnDefinition = "VARCHAR(255)")

    和(如果您需要表示关系的字段,例如@ManyToOne等@OneToOne),具体取决于要求:

    @JoinColumn(name = "backlog_item_id", columnDefinition = "VARCHAR(255)")

    1. 将依赖版本更新H2到最新(当前2.2.224):

       <dependency>
           <groupId>com.h2database</groupId>
           <artifactId>h2</artifactId>
           <version>2.2.224</version>
       </dependency>
      
    2. application.properties使用jdbc:h2:file格式更新选项:

    jdbc:h2:文件:./data/testdb;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE

    • 0

相关问题

  • wpcap 找不到指定的模块

  • 如何以编程方式从桌面应用程序打开 HTML 页面?

  • Android Studio 中的 R.java 文件在哪里?

  • HashMap 初始化

  • 如何使用 lambda 表达式通过增加与原点的距离来对点进行排序?

  • 最大化窗口时如何调整元素大小?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5