Sorry if this has been covered elsewhere. I've been trying to use 
http://orientdb.com/docs/2.0/orientdb.wiki/Object-2-Record-Java-Binding.html 
and StackOverflow to help me understand how to construct a POC for storing 
domain objects in document or graph form. (I recognize that there is Frames 
support, but that's not POJO, and there's a code base of POJOs already in 
place. We'll investigate that later.)

According to the documentation, the following should work. The issue is the 
embedded List<:>, which always seems to be null in the proxied object, 
causing NPE.

I've attached the test class and also the server config XML, which is the 
same as the installed default with the addition of a "root" user. My test 
environment is 

   - OrientDB Community Edition v 2.0.10 
   - JDK 1.8.0_25, 
   - Window 7 Pro SP1, 
   - run from within IntelliJ IDEA 14.1.3


Any insight would probably be useful to reflect on the OrientDB 
documentation pages as well. There seems to be a lot of tribal knowledge 
scattered on StackOverflow and on this group.

Thanks for your help!


package project.ogm.simplest;

import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.entity.OEntityManager;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import com.orientechnologies.orient.object.iterator.OObjectIteratorClass;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.OServerMain;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import javax.persistence.Embedded;

/**
 * Attempts to follow guidelines in 
http://orientdb.com/docs/2.0/orientdb.wiki/Object-2-Record-Java-Binding.html
 */
public class BasicOrientPojoTest {
  static public class KNode {

    @Embedded  // According to 
http://stackoverflow.com/questions/26118820/orientdb-pojo-mapping-with-embedded-objects
    private List<String> args;

    // has no setter, but is serialized anyway. Good.
    Object value;

    public KNode(List<String> args, Object value) {
      this.args = args;
      this.value = value;
    }

    public KNode(Object value) {
      this.args = Collections.emptyList();
      this.value = value;
    }

    /** for deserialization only */
    protected KNode() {  }

    public Object getValue() {
      return value;
    }

    public int countArgs() {
      return args.size();
    }

    public String getArg(int i) {
      return args.get(i);
    }

    // getter and setter here only exposed to induce serialization per 
http://orientdb.com/docs/2.0/orientdb.wiki/Object-2-Record-Java-Binding.html
    public List<String> getArgs () {
      return args;
    }

    public void setArgs(List<String> args) {
      this.args = args;
    }
  }

  /**
   * Ensures clean database created
   *
   * @param user
   * @param password
   * @param dbDir name of db to create (will be a dir somewhere)
   * @throws IOException
   */
  private void ensureCleanDatabase(String user, String password, String dbDir) 
throws IOException {
    String connectUrl = "remote:localhost" + "/" + dbDir;
    OServerAdmin serverAdmin = new OServerAdmin(connectUrl).connect(user, 
password);
    try {
      if (serverAdmin.existsDatabase("plocal")) {
        serverAdmin.dropDatabase("plocal");
      }

      serverAdmin.createDatabase(dbDir, "graph", "plocal");
    }
    finally {
      serverAdmin.close();
    }
  }

  static OObjectDatabaseTx openDb(String dbDir, String user, String password) {
    OObjectDatabaseTx db = new OObjectDatabaseTx ("plocal:" + dbDir).open(user, 
password);
    OEntityManager em = db.getEntityManager();
    // just get this one class in!
    em.registerEntityClass(KNode.class);
    // doesn't work with these, doesn't work without either. No idea if 
necessary.
    em.registerEntityClass(ArrayList.class);
    em.registerEntityClass(List.class);

    return db;
  }


  @Test
  public void smokeTestAPojo() throws Exception {
    String dbDir = "KNodes";

    OServer server = OServerMain.create();
    try {
      
server.startup(getClass().getResourceAsStream("/orientdb-server-test.xml"));
      server.activate();

      ensureCleanDatabase("root", "root", dbDir);

      {
        OObjectDatabaseTx db = openDb(dbDir, "admin", "admin");
        try {
          KNode kna = new KNode("a");
          KNode resultA = db.attachAndSave(kna);
          assertEquals("a", resultA.getValue());
          //! NPE on next line
          assertEquals(0, resultA.countArgs());

          KNode knb = new KNode("b");
          KNode resultB = db.attachAndSave(knb);
          assertEquals("b", resultB.getValue());
          assertEquals(0, resultA.countArgs());

          ArrayList<String> args = new ArrayList<>();
          args.add("c1");
          args.add("c2");
          KNode knc = new KNode(
            args,
            "c"
          );
          KNode resultC = db.attachAndSave(knc);
          assertEquals("c", resultC.getValue());
          assertEquals(2, resultC.countArgs());

          db.commit();
        }
        finally {
          db.close();
        }
      }

      {
        OObjectDatabaseTx db = openDb(dbDir, "admin", "admin");
        try {
          OObjectIteratorClass<KNode> kns = db.browseClass(KNode.class, true);
          assertTrue(kns.hasNext());
          Map<Object, KNode> nodes = new HashMap<>();
          while (kns.hasNext()) {
            KNode node = kns.next();
            nodes.put(node.value, node);
          }
          assertEquals(3, nodes.size());
          KNode kn;
          assertNotNull(kn = nodes.get("a"));
          assertEquals("a", kn.getValue());
          assertEquals(0, kn.countArgs());

          assertNotNull(kn = nodes.get("c"));
          assertEquals("c", kn.getValue());
          assertEquals(2, kn.countArgs());
          assertEquals("c2", kn.getArg(1));
        } finally {
          db.close();
        }
      }
    }
    finally {
      server.shutdown();
    }
  }
}

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
package project.ogm.simplest;

import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.entity.OEntityManager;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import com.orientechnologies.orient.object.iterator.OObjectIteratorClass;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.OServerMain;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import javax.persistence.Embedded;

/**
 * Attempts to follow guidelines in http://orientdb.com/docs/2.0/orientdb.wiki/Object-2-Record-Java-Binding.html
 */
public class BasicOrientPojoTest {
  static public class KNode {

    @Embedded  // According to http://stackoverflow.com/questions/26118820/orientdb-pojo-mapping-with-embedded-objects
    private List<String> args;

    // has no setter, but is serialized anyway. Good.
    Object value;

    public KNode(List<String> args, Object value) {
      this.args = args;
      this.value = value;
    }

    public KNode(Object value) {
      this.args = Collections.emptyList();
      this.value = value;
    }

    /** for deserialization only */
    protected KNode() {  }

    public Object getValue() {
      return value;
    }

    public int countArgs() {
      return args.size();
    }

    public String getArg(int i) {
      return args.get(i);
    }

    // getter and setter here only exposed to induce serialization per http://orientdb.com/docs/2.0/orientdb.wiki/Object-2-Record-Java-Binding.html
    public List<String> getArgs () {
      return args;
    }

    public void setArgs(List<String> args) {
      this.args = args;
    }
  }

  /**
   * Ensures clean database created
   *
   * @param user
   * @param password
   * @param dbDir name of db to create (will be a dir somewhere)
   * @throws IOException
   */
  private void ensureCleanDatabase(String user, String password, String dbDir) throws IOException {
    String connectUrl = "remote:localhost" + "/" + dbDir;
    OServerAdmin serverAdmin = new OServerAdmin(connectUrl).connect(user, password);
    try {
      if (serverAdmin.existsDatabase("plocal")) {
        serverAdmin.dropDatabase("plocal");
      }

      serverAdmin.createDatabase(dbDir, "graph", "plocal");
    }
    finally {
      serverAdmin.close();
    }
  }

  static OObjectDatabaseTx openDb(String dbDir, String user, String password) {
    OObjectDatabaseTx db = new OObjectDatabaseTx ("plocal:" + dbDir).open(user, password);
    OEntityManager em = db.getEntityManager();
    // just get this one class in!
    em.registerEntityClass(KNode.class);
    // doesn't work with these, doesn't work without either. No idea if necessary.
    em.registerEntityClass(ArrayList.class);
    em.registerEntityClass(List.class);

    return db;
  }


  @Test
  public void smokeTestAPojo() throws Exception {
    String dbDir = "KNodes";

    OServer server = OServerMain.create();
    try {
      server.startup(getClass().getResourceAsStream("/orientdb-server-test.xml"));
      server.activate();

      ensureCleanDatabase("root", "root", dbDir);

      {
        OObjectDatabaseTx db = openDb(dbDir, "admin", "admin");
        try {
          KNode kna = new KNode("a");
          KNode resultA = db.attachAndSave(kna);
          assertEquals("a", resultA.getValue());
          //! NPE on next line
          assertEquals(0, resultA.countArgs());

          KNode knb = new KNode("b");
          KNode resultB = db.attachAndSave(knb);
          assertEquals("b", resultB.getValue());
          assertEquals(0, resultA.countArgs());

          ArrayList<String> args = new ArrayList<>();
          args.add("c1");
          args.add("c2");
          KNode knc = new KNode(
            args,
            "c"
          );
          KNode resultC = db.attachAndSave(knc);
          assertEquals("c", resultC.getValue());
          assertEquals(2, resultC.countArgs());

          db.commit();
        }
        finally {
          db.close();
        }
      }

      {
        OObjectDatabaseTx db = openDb(dbDir, "admin", "admin");
        try {
          OObjectIteratorClass<KNode> kns = db.browseClass(KNode.class, true);
          assertTrue(kns.hasNext());
          Map<Object, KNode> nodes = new HashMap<>();
          while (kns.hasNext()) {
            KNode node = kns.next();
            nodes.put(node.value, node);
          }
          assertEquals(3, nodes.size());
          KNode kn;
          assertNotNull(kn = nodes.get("a"));
          assertEquals("a", kn.getValue());
          assertEquals(0, kn.countArgs());

          assertNotNull(kn = nodes.get("c"));
          assertEquals("c", kn.getValue());
          assertEquals(2, kn.countArgs());
          assertEquals("c2", kn.getArg(1));
        } finally {
          db.close();
        }
      }
    }
    finally {
      server.shutdown();
    }
  }
}

Attachment: orientdb-server-test.xml
Description: XML document

Reply via email to