Spring Boot应用程序-H2数据库(1.4.200)数组列,Hibernate映射
我正在创建一个Spring Boot应用程序,其中有一个路由表。在路线表中,我有停止列,我想存储停止ID的整数阵列。
由于H2数据库(1.4.200版)不支持键入的数组,因此我将其定义为通用数组。
CREATE TABLE Routes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(250) NOT NULL,
stops ARRAY[100] NOT NULL
);
INSERT INTO Routes (id, name, stops) VALUES (1, '500CK', (1,2));
SELECT * FROM Routes;
ID NAME STOPS
1 500CK [1, 2]
我想知道什么应该是停止属性的类型(需要的任何其他配置),因此它可以与Hibernate一起使用,而我
@Entity
@Table(name = "Routes")
public class Route {
@Id
@GeneratedValue
private int id;
private String name;
// I'm not sure what should be the data type of stops
private Object[] stops;
}
在尝试从 /路由端点< /strong>的路由时,我会遇到错误,
org.h2.jdbc.JdbcSQLDataException: Hexadecimal string contains non-hex character: "[1, 2]" [90004-200]
我创建了路由表通过在 schema.sql 文件中定义DDL
DROP TABLE IF EXISTS Routes;
CREATE TABLE Routes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(250) NOT NULL,
stops ARRAY[100] NOT NULL
);
,然后通过在 data.sql file 更新中定义DML来插入一个记录
INSERT INTO ROUTES (id, name, stops) VALUES
(1, '500CK', (1,2));
:
我更新了H2至2.1.212
CREATE TABLE Routes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(250) NOT NULL,
stops INTEGER ARRAY[100] NOT NULL
);
INSERT INTO ROUTES (id, name, stops) VALUES(1, '500CK', ARRAY[1, 2]);
我可以在路由表中看到上述记录。
更改了路由实体,因为下面
@Entity
@Table(name = "Routes")
public class Route {
@Id
@GeneratedValue
private int id;
private String name;
private Integer[] stops;
// getter & setters
}
我要低于错误,
org.h2.jdbc.JdbcSQLDataException: Data conversion error converting "ARRAY to BINARY VARYING" [22018-212]
我正在使用Hibernate-Core 5.6.8。最终
看起来像Hibernate无法进行转换。
我可以只用冬眠而实现,还是必须这样做,在这里提到的方式
https://vladmihalcea.com/how-to-map-java--java-and-sql-sql-arreays-with-jpa-and-hibernate/
更新:尝试将Hibernate-Core更新为6.1.0.-Final
我试图将Hibernate-Core更新为6.1.0.-final。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.0.Final</version>
</dependency>
但是我要低于错误
Field routeRepository in net.mahtabalam.service.RouteService required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
看来这是由于依赖版本冲突而引起的
。
<?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.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.mahtabalam</groupId>
<artifactId>routes</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>routes</name>
<description>Bus Routes</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.212</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I'm creating a Spring Boot application, where I have a Routes table. Within the Routes table I have stops column where I want to store the integer array of stop id.
Since H2 database (version 1.4.200) doesn't support typed array, I defined it as generic array.
CREATE TABLE Routes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(250) NOT NULL,
stops ARRAY[100] NOT NULL
);
INSERT INTO Routes (id, name, stops) VALUES (1, '500CK', (1,2));
SELECT * FROM Routes;
ID NAME STOPS
1 500CK [1, 2]
I want to know what should be the type of stops property (any other configuration required) so it works with Hibernate
@Entity
@Table(name = "Routes")
public class Route {
@Id
@GeneratedValue
private int id;
private String name;
// I'm not sure what should be the data type of stops
private Object[] stops;
}
I'm getting below error while trying to get the route from /route endpoint
org.h2.jdbc.JdbcSQLDataException: Hexadecimal string contains non-hex character: "[1, 2]" [90004-200]
I created the Routes table by defining the DDL in the schema.sql file
DROP TABLE IF EXISTS Routes;
CREATE TABLE Routes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(250) NOT NULL,
stops ARRAY[100] NOT NULL
);
And I inserted one record by defining the DML in data.sql file
INSERT INTO ROUTES (id, name, stops) VALUES
(1, '500CK', (1,2));
UPDATE :
I updated h2 to 2.1.212
CREATE TABLE Routes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(250) NOT NULL,
stops INTEGER ARRAY[100] NOT NULL
);
INSERT INTO ROUTES (id, name, stops) VALUES(1, '500CK', ARRAY[1, 2]);
I can see the above record in the Routes table.
Changed the Route entity as below
@Entity
@Table(name = "Routes")
public class Route {
@Id
@GeneratedValue
private int id;
private String name;
private Integer[] stops;
// getter & setters
}
I'm getting below error
org.h2.jdbc.JdbcSQLDataException: Data conversion error converting "ARRAY to BINARY VARYING" [22018-212]
I'm using hibernate-core 5.6.8.Final
Looks like Hibernate is not able to do the conversion.
Can I achieve it with just Hibernate, Or do I have to do it, the way mentioned here
https://vladmihalcea.com/how-to-map-java-and-sql-arrays-with-jpa-and-hibernate/
Update : Trying to update hibernate-core to 6.1.0.Final
I tried to update the hibernate-core to 6.1.0.Final.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.0.Final</version>
</dependency>
But I'm getting below error
Field routeRepository in net.mahtabalam.service.RouteService required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
Looks like this is caused due to dependency version conflicts.
Below is the project pom.xml
<?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.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.mahtabalam</groupId>
<artifactId>routes</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>routes</name>
<description>Bus Routes</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.212</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,据我所知,目前将无法使用Hibernate 6.1.0。最终使用Spring Boot。您在EntityManagerFactory中注意到的问题将是因为Hibernate-entityManager项目已在6.1.0.-final版本中移至Hibernate-Core。
查看Spring Boot的计划,请参阅此处,似乎没有任何即将更新冬眠版本的计划,因为要等到版本3的M3才能更新。
相反,最好的选择是制作自定义转换器,我目前正在尝试解决完全相同的问题,如果我找到解决方案,我将分享。
Unfortunately to my knowledge there will be no way to use hibernate 6.1.0.Final with Spring Boot currently. The issue you've noted with entityManagerFactory will be because the hibernate-entitymanager project was moved into hibernate-core in version 6.1.0.Final.
Looking through the plans for Spring Boot, see here, it looks like there is no imminment plan to update the version of hibernate used either as it won't be until M3 of version 3.
Instead your best bet will be to make a custom converter, I am currently trying to tackle the exact same problem and if I find a solution I will share it.