OrientDB - exception when I try to save an embeddedlist

131 Views Asked by At

I have a class named CalculationFunctionGroup where I have an attribute like this:

List<CalculationFunction> functions;

on my OrientDB I have a table named CalculationFunctionGroup with a property functions definaed as EMBEDDEDLIST and linked class CalculationFunction.

When I try to save an object of type CalculationFunctionGroup an exception raises.

The exception tell me:

The field 'functions' has been declared as EMBEDDEDLIST with linked class 'CalculationFunction' but the record has no class.

I try to find this exception in OrientDB source code, and I find this:

There's a check in ODocument class in the method validateEmbedded where there are these code lines:

if (doc.getImmutableSchemaClass() == null)
      throw new OValidationException("The field '" + p.getFullName() + "' has been declared as " + p.getType()
          + " with linked class '" + embeddedClass + "' but the record has no class");

So, I don't understand how can I valued the immutableschemaclass property.

Where I try to set my field value from Java, I use this command line:

this.data.field(fieldName, value, OType.EMBEDDEDLIST);

where data is my ODocument instance, fieldName is functions, value is my List of CalculationFunction and OType is EMBEDDEDLIST.

Used Orient version is 2.2.0

EDIT #1

I try this after Alessandro Rota answer, but the error is the same:

ODocument myEntity = new ODocument("CalculationFunctionGroup");
myEntity.field("referenceItem", object.getReferenceItem().getData());
db.save(myEntity);
db.commit();

In this code snippet I've changed the nature of my objct (the original is a typied object as CalculationFunctionGroup and now is an ODocument). But the error is the same.

Another try I've done, the ODocument myEntity has not attached functions (list of CalculationFunction) but the error raises too

EDIT #2

I've tried with code snippet of Alessandro Rota and works fine.

But when I add as field of CalculationFunction a link field I've the same error! Why?

If I add a link field instead of object.getData() with object.getRawField("@rid") it works fine too.

I don't understand because raises that error message, and the reason of different behaviour when I use only @rid field instead complete object

EDIT #3 Latest news:

This is my test scenario:

I have this table:

CalculationFunction with these property (schemafull):

referenceItem LINK functions EMBEDDEDLIST

When I try to save, I write this code:

    ODocument myGroup = new ODocument("CalculationFunctionGroup");
    Object rid = null;
    if (object.getField("referenceItem") instanceof RegistryMetadata) {
        rid = ((RegistryMetadata)(object.getField("referenceItem"))).getRawField("@rid");
    } else if (object.getField("referenceItem") instanceof PayrollRegistryMetadata) {
        rid = ((PayrollRegistryMetadata)(object.getField("referenceItem"))).getRawField("@rid");
    } else if (object.getField("referenceItem") instanceof PreCalculationMetadata) {
        rid = ((PreCalculationMetadata)(object.getField("referenceItem"))).getRawField("@rid");
    } else if (object.getField("referenceItem") instanceof CalculationMetadata) {
        rid = ((CalculationMetadata)(object.getField("referenceItem"))).getRawField("@rid");
    }

    myGroup.field("referenceItem", rid, OType.LINK);
    myGroup.field("scenario", ((Scenario)object.getField("scenario")).getRawField("@rid"));

    List<ODocument> lstFunctions = new ArrayList<ODocument>();
    if (object.getField("functions") != null) {
        Iterable<ODocument> lstFun = (Iterable<ODocument>) object.getField("functions");
        Iterator<ODocument> itFun = lstFun.iterator(); 
        while(itFun.hasNext()) {
            ODocument currFun = itFun.next();

            ODocument oFun = new ODocument("CalculationFunction");
            oFun.field("name", currFun.field("name"));
            oFun.field("code", currFun.field("code"));
            oFun.field("language", currFun.field("language"));

            lstFunctions.add(oFun);
        }
    }
    myGroup.field("functions", lstFunctions, OType.EMBEDDEDLIST);
    myGroup.save();

    db.commit();

This code goes in error, but... If I write this:

    myGroup.field("referenceItem2", rid, OType.LINK);

The code works fine.

The difference: referenceItem is a schemafull property, referenceItem2 is a schemaless field.

1

There are 1 best solutions below

6
On

You could use this code

ODocument cf1= new ODocument("CalculationFunction");
cf1.field("name","Function 1",OType.STRING);

ODocument cf2= new ODocument("CalculationFunction");
cf2.field("name","Function 2",OType.STRING);

List<ODocument> list=new ArrayList<ODocument>();
list.add(cf1);
list.add(cf2);

ODocument p = new ODocument("CalculationFunctionGroup");
p.field("functions", list, OType.EMBEDDEDLIST);
p.save();

enter image description here

Edit 2

If you want add a link you could use this code

ODocument cf1= new ODocument("CalculationFunction");
cf1.field("name","Function 1",OType.STRING);

ODocument p = new ODocument("CalculationFunctionGroup");
p.field("mylink", cf1, OType.LINK);
p.save();

EDIT 3

I have created schemafull property mylink2

enter image description here

I have used this code

ODocument cf1= new ODocument("CalculationFunction");
cf1.field("name","Function 1",OType.STRING);
cf1.save();

ODocument p = new ODocument("CalculationFunctionGroup");
p.field("mylink", cf1.getIdentity(), OType.LINK);
p.field("mylink2", cf1.getIdentity(), OType.LINK);
p.save();

and I got

enter image description here

Hope it helps.