orientdb 属性 像 MANDATORY 这样的约束不会因不正确的 java api 插入而失败
orientdb property constraints like MANDATORY not failing on improper java api insertions
我有一个带有 class 的 orientdb 设置,名为 MessageLog,带有 属性 messageId,它具有 MANADATORY 和 NOT NULL 约束。如果我尝试从控制台插入一条带有空 messageId 的记录,它会引发异常,告知 属性 不能为空。但是,当我从 Java API 中进行简单插入时,插入的记录的 属性 值为 null。怎么可能。
java代码是:
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
public class OrientDbTrials {
public static void main(String[] args) {
OrientGraph graph = new OrientGraph("remote:localhost/blah","root","*****");
System.out.println("Connected to the db.");
Vertex messageLog = graph.addVertex("class:MessageLog");
System.out.println("Created new vertex : " + messageLog.toString());
messageLog.setProperty("messageId", null);
graph.commit();
System.out.println("Successfully saved it.");
}
}
谁能解释一下。
来自http://www.orientechnologies.com/docs/1.7.8/orientdb.wiki/Graph-Database-Tinkerpop.html
Every time the graph is modified an implicit transaction is started automatically if no previous transaction was running. Transactions are committed automatically when the graph is closed by calling the shutdown() method or by explicit commit(). To rollback changes call the rollback() method. Changes inside a transaction will be temporary till the commit or the close of the graph instance. Concurrent threads or external clients can see the changes only when the transaction has been fully committed.
因为您没有定义如何处理异常,所以我认为当出现一个异常时,图形将被关闭并因此提交更改。当我 运行 您在 try catch 块中提供的代码时,出现异常。
OrientGraph graph = new OrientGraph("remote:localhost/blah","root","*****");
System.out.println("Connected to the db.");
try { Vertex messageLog = graph.addVertex("class:MessageLog");
System.out.println("Created new vertex : " + messageLog.toString());
messageLog.setProperty("messageId", null);
graph.commit();
} catch(Exception e) {
graph.rollback();
}
finally {
graph.shutdown();
}
该代码(请原谅丑陋的代码)引发此异常
java.lang.IllegalArgumentException: Property value can not be null
因为我用 graph.rollback() 处理异常,所以顶点没有出现在图中。
有趣的是,仅具有强制性 属性 似乎还不够,即,如果您从不首先用 null 填充该字段,它将允许提交,而不是引发异常.我没有研究过它,但也许它与默认情况下混合架构的图形实例或类似的东西有关?我希望其他人知道。
我有一个带有 class 的 orientdb 设置,名为 MessageLog,带有 属性 messageId,它具有 MANADATORY 和 NOT NULL 约束。如果我尝试从控制台插入一条带有空 messageId 的记录,它会引发异常,告知 属性 不能为空。但是,当我从 Java API 中进行简单插入时,插入的记录的 属性 值为 null。怎么可能。
java代码是:
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
public class OrientDbTrials {
public static void main(String[] args) {
OrientGraph graph = new OrientGraph("remote:localhost/blah","root","*****");
System.out.println("Connected to the db.");
Vertex messageLog = graph.addVertex("class:MessageLog");
System.out.println("Created new vertex : " + messageLog.toString());
messageLog.setProperty("messageId", null);
graph.commit();
System.out.println("Successfully saved it.");
}
}
谁能解释一下。
来自http://www.orientechnologies.com/docs/1.7.8/orientdb.wiki/Graph-Database-Tinkerpop.html
Every time the graph is modified an implicit transaction is started automatically if no previous transaction was running. Transactions are committed automatically when the graph is closed by calling the shutdown() method or by explicit commit(). To rollback changes call the rollback() method. Changes inside a transaction will be temporary till the commit or the close of the graph instance. Concurrent threads or external clients can see the changes only when the transaction has been fully committed.
因为您没有定义如何处理异常,所以我认为当出现一个异常时,图形将被关闭并因此提交更改。当我 运行 您在 try catch 块中提供的代码时,出现异常。
OrientGraph graph = new OrientGraph("remote:localhost/blah","root","*****");
System.out.println("Connected to the db.");
try { Vertex messageLog = graph.addVertex("class:MessageLog");
System.out.println("Created new vertex : " + messageLog.toString());
messageLog.setProperty("messageId", null);
graph.commit();
} catch(Exception e) {
graph.rollback();
}
finally {
graph.shutdown();
}
该代码(请原谅丑陋的代码)引发此异常
java.lang.IllegalArgumentException: Property value can not be null
因为我用 graph.rollback() 处理异常,所以顶点没有出现在图中。
有趣的是,仅具有强制性 属性 似乎还不够,即,如果您从不首先用 null 填充该字段,它将允许提交,而不是引发异常.我没有研究过它,但也许它与默认情况下混合架构的图形实例或类似的东西有关?我希望其他人知道。