Spring数据:@NosqlTable添加Map变量



我正试图在我的oracle nosqltable中添加一个Map字段(在这里给出的示例中https://docs.oracle.com/en/database/other-databases/nosql-database/21.1/java-driver-table/accessing-nosql-using-sdf.html)但在保存时,Spring数据没有正确保存它。

客户.java

@NosqlTable(storageGB = 1, writeUnits = 10, readUnits = 10)
public class Customer {
@NosqlId(generated = true)
long customerId;
String firstName;
String lastName;
Map hashMap;
Date createdAt;
@Override
public String toString() {
return "Customer{" +
"customerId=" + customerId +
", firstName='" + firstName + ''' +
", lastName='" + lastName + ''' +
", createdAt='" + createdAt + ''' +
", hashMap='" + hashMap + ''' +
'}';
}
}

客户报告.java

import com.oracle.nosql.spring.data.repository.NosqlRepository;
public interface CustomerRepository
extends NosqlRepository<Customer, Long>
{
Iterable<Customer> findByLastName(String lastname);
}

当我调用以下代码创建客户行时:

Customer s1 = new Customer();
s1.firstName = "John";
s1.lastName = "Doe";
HashMap s1Map = new HashMap() ;
s1Map.put("name", "myMap") ;
s1Map.put("use", true);
s1.hashMap = s1Map;
repo.save(s1);

它被保存为

{
"createdAt": null,
"firstName": "John",
"hashMap": {
"entrySet": null,
"keySet": null,
"loadFactor": 0.75,
"modCount": 2,
"size": 2,
"table": [
null,
null,
null,
null,
null,
null,
{
"hash": 116102,
"key": "use",
"next": null,
"value": true
},
null,
{
"hash": 3373752,
"key": "name",
"next": null,
"value": "myMap"
},
null,
null,
null,
null,
null,
null,
null
],
"threshold": 12,
"values": null
},
"lastName": "Doe"
}

有人能帮我提供用于nosql映射的正确数据类型吗?

Map(java.util.Map(当前不受支持,但它是关键路线图项

看这里https://github.com/oracle/nosql-spring-sdk/issues/18

您可以在文档中找到当前Java和NoSQL JSON类型之间的映射-https://docs.oracle.com/en/database/other-databases/nosql-database/22.2/springsdk/persistence-model.html

同时,可以使用类oracle.nosql.driver.values.MapValue。以下是的示例

import com.oracle.nosql.spring.data.core.mapping.NosqlId;
import oracle.nosql.driver.values.MapValue ;
@NosqlTable(storageGB = 1, writeUnits = 10, readUnits = 10)
public class Customer {
@NosqlId(generated = true)
long customerId;
String firstName;
String lastName;
MapValue map;
@Override
public String toString() {
return "Customer{" +
"customerId=" + customerId +
", firstName='" + firstName + ''' +
", lastName='" + lastName + ''' +
", map='" + map + ''' +
'}';
}
}

下面是一个调用的示例,用于创建客户行-使用多种类型,包括其他nosqlClasseoracle.nosql.driver.values.ArrayValue。正如您所看到的,语法与java.util.Map非常相似。当支持java.util.Map时,您可以轻松迁移

Customer c2 = new Customer();
c2.firstName = "John";
c2.lastName = "Josh";
c2.map = new MapValue();
c2.map.put("number field", 1);
c2.map.put("string_field", "string value");
c2.map.put("boolean_field", true);
ArrayValue arrayValue = new ArrayValue();
arrayValue.add(100);
arrayValue.add("102");
arrayValue.add(true);
c2.map.put("json-field", arrayValue);
repo.save(c2);
System.out.println("nsaved: " + c2); 
System.out.println("nfindAll:");
Iterable<Customer> customers = repo.findAll();
for (Customer s : customers) {
System.out.println("  Customer: " + s);
}
System.out.println("nfindByLastName: Josh");
customers = repo.findByLastName("Josh");
for (Customer s : customers) {
System.out.println("  Customer: " + s);
}

以下是输出-应用

saved: Customer{customerId=10, firstName='John', lastName='Doe', map='null'}
saved: Customer{customerId=11, firstName='John', lastName='Smith', map='null'}
saved: Customer{customerId=12, firstName='John', lastName='Josh', map='{"number field":1,"json-field":[100,"102",true],"string_field":"string value"}'}
findAll:
Customer: Customer{customerId=12, firstName='John', lastName='Josh', map='{"json-field":[100,"102",true],"number field":1,"string_field":"string value"}'}
Customer: Customer{customerId=10, firstName='John', lastName='Doe', map='null'}
Customer: Customer{customerId=11, firstName='John', lastName='Smith', map='null'}
findByLastName: Josh
Customer: Customer{customerId=12, firstName='John', lastName='Josh', map='{"json-field":[100,"102",true],"number field":1,"string_field":"string value"}'}

阅读使用SQL for Oracle NoSQL数据库外壳-如果是Cloud,您可以使用OCI控制台

sql-> mode json -pretty
Query output mode is pretty JSON
sql-> select * from customer;
{
"customerId" : 13,
"kv_json_" : {
"firstName" : "John",
"lastName" : "Doe",
"map" : null
}
}
{
"customerId" : 14,
"kv_json_" : {
"firstName" : "John",
"lastName" : "Smith",
"map" : null
}
}
{
"customerId" : 15,
"kv_json_" : {
"firstName" : "John",
"lastName" : "Josh",
"map" : {
"boolean_field" : true,
"json-field" : [100, "102", true],
"number field" : 1,
"string_field" : "string value"
}
}
}

为了运行此示例,您需要应用修复程序,否则,您将出现以下错误

Caused by: java.lang.IllegalArgumentException: Entity must not be null!

修复程序将很快在此GitHub存储库上发布https://github.com/oracle/nosql-spring-sdk.再次点击此处了解更多信息https://github.com/oracle/nosql-spring-sdk/issues/18

了解有关MapValue 的更多信息

  • https://oracle.github.io/nosql-java-sdk/oracle/nosql/driver/values/MapValue.html
  • https://oracle.github.io/nosql-java-sdk/oracle/nosql/driver/values/FieldValue.html

Map(java.util.Map(在1.5版本中受支持。

看这里https://github.com/oracle/nosql-spring-sdk/blob/main/CHANGELOG.md#150

最新更新