我有一个 oracle 表
MYTABLE
,它有 3 列,如下所述。
|id | myclob_column | column3|
它有一列(
myclob_column
)是clob数据(json)。示例 clob 数据如下。
{
"id" : 10001
"name" : "Rahul",
"keyvalue" : [ {"key" : "100", "value" : "A"}, {"key" : "200", "value" : "B"} .....]
}
KeyValue 在我的项目中被定义为一个类,如下所示。
@Data
public class KeyValue {
private String key;
private String value;
}
我在
org.hibernate.annotations.Formula
和name
字段上使用keyvalue
注释。
@Data
@Entity
@Table(name = "MYTABLE")
public class MyEntity {
@Id
private String id;
@Formula("JSON_VALUE(myclob_column, '$.name')")
private String name;
@Formula("JSON_VALUE(myclob_column, '$.keyvalue[*]')")
private List<KeyValue> keyvalue;
}
在运行
Spring Boot
应用程序时,我收到 keyvalue
字段的错误,并且应用程序没有为此启动。
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: MYTABLE, for columns: [org.hibernate.mapping.Formula(JSON_VALUE(myclob_column, '$.keyvalue[*]'))]
你能提出什么问题吗?
好的,所以如果您使用的是 Hibernate 5,那么根本就没有将 JSON 映射到
List
s 的内置支持,我真的不确定是什么给您留下了这样的印象。
原则上,你可以自己写一个
UserType
来处理这个问题。 (甚至可以使用 JPA AttributeConverter
解决问题,但如果不亲自尝试,我无法确定。)
现在,在 Hibernate 6 中,情况有点little 不同,实际上支持将一些集合映射到 JSON。 例如,支持以下:
@JdbcTypeCode(JSON)
List<String> point;
但是,从 Hibernate 6.2 开始,元素类型必须是基本类型。尚不支持映射,例如
List<KeyValue>
其中 KeyValue
是可嵌入类型。我 do 希望 Christian 或其他人会在 eventually 上工作,但今天不存在。
所以简短的回答是,任何版本的 Hibernate 都不直接支持您尝试做的事情,但是您应该能够使用
UserType
.