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

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

Inserting into embedded list of orient db through java

问题

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

CREATE CLASS ProductSummary;
CREATE PROPERTY ProductSummary.name STRING (NOTNULL, MANDATORY TRUE);
CREATE PROPERTY ProductSummary.modelNumber LONG (NOTNULL, MANDATORY TRUE);
ALTER CLASS ProductSummary STRICTMODE TRUE;

CREATE CLASS PricingSummary;
CREATE PROPERTY PricingSummary.price LONG (NOTNULL, MANDATORY TRUE);
CREATE PROPERTY PricingSummary.discount LONG (NOTNULL, MANDATORY TRUE);
ALTER CLASS PricingSummary STRICTMODE TRUE;

CREATE CLASS TotalSummary EXTENDS V;
CREATE PROPERTY TotalSummary.projectLink LINK Project (NOTNULL, MANDATORY TRUE);
CREATE PROPERTY TotalSummary.productSummaries EMBEDDEDLIST ProductSummary;
CREATE PROPERTY TotalSummary.pricingSummaries EMBEDDEDLIST PricingSummary;
ALTER CLASS TotalSummary STRICTMODE TRUE;
CREATE INDEX TotalSummary_projectLink_idx ON TotalSummary (projectLink) UNIQUE;

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

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

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

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

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

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

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

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

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 :

CREATE CLASS ProductSummary;
CREATE PROPERTY ProductSummary.name STRING (NOTNULL, MANDATORY TRUE);
CREATE PROPERTY ProductSummary.modelNumber LONG (NOTNULL, MANDATORY TRUE);
ALTER CLASS ProductSummary STRICTMODE TRUE;
 
CREATE CLASS PricingSummary;
CREATE PROPERTY PricingSummary.price LONG (NOTNULL, MANDATORY TRUE);
CREATE PROPERTY PricingSummary.discount LONG (NOTNULL, MANDATORY TRUE);
ALTER CLASS PricingSummary STRICTMODE TRUE;
 
CREATE CLASS TotalSummary EXTENDS V;
CREATE PROPERTY TotalSummary.projectLink LINK Project (NOTNULL, MANDATORY TRUE);
CREATE PROPERTY TotalSummary.productSummaries EMBEDDEDLIST ProductSummary;
CREATE PROPERTY TotalSummary.pricingSummaries EMBEDDEDLIST PricingSummary;
ALTER CLASS TotalSummary STRICTMODE TRUE;
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.

public TotalSummary create(final TotalSummary totalSummary) {
		final Long projectId = 1;
		final StatementBuilder builder = new StatementBuilder();
		final StringBuilder query = new StringBuilder();
		
		final List&lt;Map&lt;?, ?&gt;&gt; productSummaries = totalSummary.getProductSummaries().stream()
				.map(ProductSummary::toMap)
				.collect(Collectors.toList());
		
		final List&lt;Map&lt;?, ?&gt;&gt; pricingSummaries = totalSummary.getPricingSummaries().stream()
				.map(PricingSummary::toMap)
				.collect(Collectors.toList());
		
		
		builder.addAttribute(&quot;projectLink = (SELECT FROM project WHERE id = ?)&quot;, projectId);
		if ( ! productSummaries.isEmpty()) {
			builder.addAttribute(&quot;productSummaries = ?&quot;, productSummaries);
		}
		if ( ! pricingSummaries.isEmpty()) {
			builder.addAttribute(&quot;pricingSummaries = ?&quot;, pricingSummaries);
		}
		
		try {
			insert(TotalSummary.class.getSimpleName(), builder.attributes(), statement -&gt; {
				builder.init(statement);
				return statement;
			});
		} catch (final UncategorizedSQLException e) {
			throw new ConstraintViolationException(totalSummary, ExceptionUtils.getRootCauseMessage(e), e);
		}

		return assertNotNull(findById(projectId));
	}

This is the utility method that I am using to build the insert query : 
protected String insert(final String vertex, final String fieldValuePairs, final PreparedStatementInitializer initializer) {
	final String sql = &quot;INSERT INTO &quot; + vertex + &quot; SET &quot; + fieldValuePairs + &quot; RETURN @rid&quot;;
	return executeStatement(sql, initializer);
}

The toMap methods to convert the List&lt;ProductSummary&gt; to List&lt;Map&lt;?,?&gt;&gt;

**ProductSummary**	
public Map&lt;String, Object&gt; toMap() {
	final Map&lt;String, Object&gt; ret = new HashMap&lt;&gt;();
	ret.put(&quot;name&quot;, name);
	ret.put(&quot;model number&quot;, Long.valueOf(modelNumber));
	return ret;
}

The toMap methods to convert the List&lt;PricingSummary&gt; to List&lt;Map&lt;?,?&gt;&gt;
**PricingSummary**
public Map&lt;String, Object&gt; toMap() {
	final Map&lt;String, Object&gt; ret = new HashMap&lt;&gt;();
	ret.put(&quot;price&quot;, Long.valueOf(price));
	ret.put(&quot;discount&quot;, Long.valueOf(discount));
	return ret;
}


I am getting the following exception when I execute the code

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 对象,例如:

OElement ret = db.newEmbeddedElement("ProductSummary");
ret.setProperty("price", Long.valueOf(price));
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.

OElement ret = db.newEmbeddedElement(&quot;ProductSummary&quot;);
ret.setProperty(&quot;price&quot;, Long.valueOf(price));
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:

确定