插入数据到OrientDB嵌套列表中的Java代码部分。

huangapple go评论112阅读模式
英文:

Inserting into embedded list of orient db through java

问题

以下是我用于创建所需类的 SQL 脚本:

  1. CREATE CLASS ProductSummary;
  2. CREATE PROPERTY ProductSummary.name STRING (NOTNULL, MANDATORY TRUE);
  3. CREATE PROPERTY ProductSummary.modelNumber LONG (NOTNULL, MANDATORY TRUE);
  4. ALTER CLASS ProductSummary STRICTMODE TRUE;
  5. CREATE CLASS PricingSummary;
  6. CREATE PROPERTY PricingSummary.price LONG (NOTNULL, MANDATORY TRUE);
  7. CREATE PROPERTY PricingSummary.discount LONG (NOTNULL, MANDATORY TRUE);
  8. ALTER CLASS PricingSummary STRICTMODE TRUE;
  9. CREATE CLASS TotalSummary EXTENDS V;
  10. CREATE PROPERTY TotalSummary.projectLink LINK Project (NOTNULL, MANDATORY TRUE);
  11. CREATE PROPERTY TotalSummary.productSummaries EMBEDDEDLIST ProductSummary;
  12. CREATE PROPERTY TotalSummary.pricingSummaries EMBEDDEDLIST PricingSummary;
  13. ALTER CLASS TotalSummary STRICTMODE TRUE;
  14. CREATE INDEX TotalSummary_projectLink_idx ON TotalSummary (projectLink) UNIQUE;

我正在尝试向 TotalSummary 类插入一些值,同时我还需要向 pricingSummaries 和 productSummaries 的嵌入式列表中插入一些值。

以下是用于构建插入查询的实用方法:

  1. protected String insert(final String vertex, final String fieldValuePairs, final PreparedStatementInitializer initializer) {
  2. final String sql = "INSERT INTO " + vertex + " SET " + fieldValuePairs + " RETURN @rid";
  3. return executeStatement(sql, initializer);
  4. }

将 List<ProductSummary> 转换为 List<Map<?, ?>> 的 toMap 方法:

  1. // ProductSummary
  2. public Map<String, Object> toMap() {
  3. final Map<String, Object> ret = new HashMap<>();
  4. ret.put("name", name);
  5. ret.put("model number", Long.valueOf(modelNumber));
  6. return ret;
  7. }

将 List<PricingSummary> 转换为 List<Map<?, ?>> 的 toMap 方法:

  1. // PricingSummary
  2. public Map<String, Object> toMap() {
  3. final Map<String, Object> ret = new HashMap<>();
  4. ret.put("price", Long.valueOf(price));
  5. ret.put("discount", Long.valueOf(discount));
  6. return ret;
  7. }

当我执行代码时,我遇到了以下异常:

  1. Constraint violation for: TotalSummary@58f79eeb[recordId=<null>,customProperties=[]]. Reason: OValidationException: The field 'TotalSummary.productSummaries' has been declared as EMBEDDEDLIST but an incompatible type is used.

我已经尝试过以下方法:

  1. 我尝试在将其添加到构建器之前将列表转换为 JSON 格式,类似于 new Gson().toJson(pricingSummaries)
  2. 将 pricingSummaries 和 productSummaries 转换为数组,使用 toArray() 方法。
英文:

This is the sql script that I have used to create the necessary classes :

  1. CREATE CLASS ProductSummary;
  2. CREATE PROPERTY ProductSummary.name STRING (NOTNULL, MANDATORY TRUE);
  3. CREATE PROPERTY ProductSummary.modelNumber LONG (NOTNULL, MANDATORY TRUE);
  4. ALTER CLASS ProductSummary STRICTMODE TRUE;
  5. CREATE CLASS PricingSummary;
  6. CREATE PROPERTY PricingSummary.price LONG (NOTNULL, MANDATORY TRUE);
  7. CREATE PROPERTY PricingSummary.discount LONG (NOTNULL, MANDATORY TRUE);
  8. ALTER CLASS PricingSummary STRICTMODE TRUE;
  9. CREATE CLASS TotalSummary EXTENDS V;
  10. CREATE PROPERTY TotalSummary.projectLink LINK Project (NOTNULL, MANDATORY TRUE);
  11. CREATE PROPERTY TotalSummary.productSummaries EMBEDDEDLIST ProductSummary;
  12. CREATE PROPERTY TotalSummary.pricingSummaries EMBEDDEDLIST PricingSummary;
  13. ALTER CLASS TotalSummary STRICTMODE TRUE;
  14. CREATE INDEX TotalSummary_projectLink_idx ON TotalSummary (projectLink) UNIQUE;

I am trying to insert some values into my TotalSummary class, where I also need to insert some values into the EmbeddedList for pricingSummaries and productSummaries.

  1. public TotalSummary create(final TotalSummary totalSummary) {
  2. final Long projectId = 1;
  3. final StatementBuilder builder = new StatementBuilder();
  4. final StringBuilder query = new StringBuilder();
  5. final List&lt;Map&lt;?, ?&gt;&gt; productSummaries = totalSummary.getProductSummaries().stream()
  6. .map(ProductSummary::toMap)
  7. .collect(Collectors.toList());
  8. final List&lt;Map&lt;?, ?&gt;&gt; pricingSummaries = totalSummary.getPricingSummaries().stream()
  9. .map(PricingSummary::toMap)
  10. .collect(Collectors.toList());
  11. builder.addAttribute(&quot;projectLink = (SELECT FROM project WHERE id = ?)&quot;, projectId);
  12. if ( ! productSummaries.isEmpty()) {
  13. builder.addAttribute(&quot;productSummaries = ?&quot;, productSummaries);
  14. }
  15. if ( ! pricingSummaries.isEmpty()) {
  16. builder.addAttribute(&quot;pricingSummaries = ?&quot;, pricingSummaries);
  17. }
  18. try {
  19. insert(TotalSummary.class.getSimpleName(), builder.attributes(), statement -&gt; {
  20. builder.init(statement);
  21. return statement;
  22. });
  23. } catch (final UncategorizedSQLException e) {
  24. throw new ConstraintViolationException(totalSummary, ExceptionUtils.getRootCauseMessage(e), e);
  25. }
  26. return assertNotNull(findById(projectId));
  27. }
  28. This is the utility method that I am using to build the insert query :
  29. protected String insert(final String vertex, final String fieldValuePairs, final PreparedStatementInitializer initializer) {
  30. final String sql = &quot;INSERT INTO &quot; + vertex + &quot; SET &quot; + fieldValuePairs + &quot; RETURN @rid&quot;;
  31. return executeStatement(sql, initializer);
  32. }
  33. The toMap methods to convert the List&lt;ProductSummary&gt; to List&lt;Map&lt;?,?&gt;&gt;
  34. **ProductSummary**
  35. public Map&lt;String, Object&gt; toMap() {
  36. final Map&lt;String, Object&gt; ret = new HashMap&lt;&gt;();
  37. ret.put(&quot;name&quot;, name);
  38. ret.put(&quot;model number&quot;, Long.valueOf(modelNumber));
  39. return ret;
  40. }
  41. The toMap methods to convert the List&lt;PricingSummary&gt; to List&lt;Map&lt;?,?&gt;&gt;
  42. **PricingSummary**
  43. public Map&lt;String, Object&gt; toMap() {
  44. final Map&lt;String, Object&gt; ret = new HashMap&lt;&gt;();
  45. ret.put(&quot;price&quot;, Long.valueOf(price));
  46. ret.put(&quot;discount&quot;, Long.valueOf(discount));
  47. return ret;
  48. }

I am getting the following exception when I execute the code

  1. Constraint violation for: TotalSummary@58f79eeb[recordId=&lt;null&gt;,customProperties=[]]. Reason: OValidationException: The field &#39;TotalSummary.productSummaries&#39; has been declared as EMBEDDEDLIST but an incompatible type is used.

Things that I have already tried :

  1. I have tried to covert to list to json format before adding it to
    the builder like so new Gson().toJson(pricingSummaries);
  2. Converted the pricingSummaries and productSummaries toArray().

答案1

得分: 1

你将摘要定义为 EMBEDDEDLIST ProductSummary,但你正在传递映射列表作为值。

尝试创建真实的 ProductSummary 对象,例如:

  1. OElement ret = db.newEmbeddedElement("ProductSummary");
  2. ret.setProperty("price", Long.valueOf(price));
  3. ret.setProperty("discount", Long.valueOf(discount));
英文:

You defined the summaries as EMBEDDEDLIST ProductSummary but you are passing lists of maps as values.

Try to create real ProductSummary objects instead eg.

  1. OElement ret = db.newEmbeddedElement(&quot;ProductSummary&quot;);
  2. ret.setProperty(&quot;price&quot;, Long.valueOf(price));
  3. ret.setProperty(&quot;discount&quot;, Long.valueOf(discount));

huangapple
  • 本文由 发表于 2020年10月16日 20:37:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64389349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定