我正在分析 Spanner 对我们现有应用程序迁移的兼容性。我在尝试使用 JPA 和 hibernate 6 将 json 数据插入到 spanner 的列中时遇到错误。 我已经提到了 Spanner JPA 的示例应用程序 以及诸如 cloud Spanner 集成 等多个帖子。但无法继续执行这些建议。需要建议来解决同样的问题。以下是错误和应用程序的详细信息
错误: 值的类型为 STRING,无法插入到列 json_data,该列的类型为 JSON
实体类:
import com.google.cloud.spanner.hibernate.PooledBitReversedSequenceStyleGenerator;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.type.SqlTypes;
import java.sql.SQLType;
import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "demo")
public class Demo {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence_generator")
@GenericGenerator(
name = "sequence_generator",
strategy = "com.google.cloud.spanner.hibernate.PooledBitReversedSequenceStyleGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = SequenceStyleGenerator.SEQUENCE_PARAM, value = "demo_sequence"),
@org.hibernate.annotations.Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "200"),
@org.hibernate.annotations.Parameter(name = SequenceStyleGenerator.INITIAL_PARAM, value = "50000"),
@org.hibernate.annotations.Parameter(name = PooledBitReversedSequenceStyleGenerator.EXCLUDE_RANGE_PARAM, value = "[1,1000]"),
})
private Long id;
@JdbcTypeCode(SqlTypes.JSON)
private Map<String, Object> jsonData;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Map<String, Object> getJsonData() {
return jsonData;
}
public void setJsonData(Map<String, Object> jsonData) {
this.jsonData = jsonData;
}
}
DDL 和 DML:
drop table IF EXISTS demo;
drop sequence IF EXISTS demo_sequence;
CREATE SEQUENCE demo_sequence OPTIONS (
sequence_kind="bit_reversed_positive"
);
create table demo(
id INT64 NOT NULL default (GET_NEXT_SEQUENCE_VALUE(SEQUENCE demo_sequence)),
json_data JSON,
)priMARY key(id);
insert into demo(json_data) values (JSON '{"name":"tom","age":30}');
POM.xml
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-hibernate-dialect</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner</artifactId>
<version>6.80.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-data-spanner</artifactId>
<version>5.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-spanner-jdbc</artifactId>
<version>2.24.1</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-60</artifactId>
<version>2.21.1</version>
</dependency>
</dependencies>