Frage

I have an embedded document based OrientDB database with records and I can't read back the saved entities. A get the right number of elements, but the attributes aren't mapped to my pojo's fields. What am I doing wrong? Any tips?

Thanks!

        OObjectDatabaseTx db = new OObjectDatabaseTx("local:db");
        if (db.exists()) {
            db = new OObjectDatabaseTx("local:db").open("admin", "admin");
        } else {
            db.create();
        }

        db.getEntityManager().registerEntityClass(User.class);

        long cnt = db.countClass(User.class);
        System.out.println(cnt); // OK

        User user = db.newInstance(User.class, "Firstname", "Lastname", "email@example.com");

        db.begin();
        db.save(user); // it's OK, the DB contains this document, I can select it with the servers console
        db.commit();

        for (User usr : db.browseClass(User.class)) {
            System.out.println(usr); // User object with null fields, why?
        }

The User class:

public class User {

private String firstname;

private String lastname;

private String email;

public User() {
}

public User(String firstname, String lastname, String email) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Override
public String toString() {
    return firstname + " " + lastname + " " + email;
}

}

War es hilfreich?

Lösung

Java objects retrieved directly from OrientDB (as you're doing in this example) are proxied transparently to their underlying records, allowing lazy loading of their fields. In your scenario, you'll have to use the User object get methods in order to trigger the population of the corresponding fields from the DB. The fields have default values (all null in your case) until loaded.

See also: "How It Works" in OrientDB Object2RecordJavaBinding

Andere Tipps

As referenced in the Binding section of the Orientdb docs, you must provide an empty constructor for each java object you wish to bind. Source : http://orientdb.com/docs/2.0/orientdb.wiki/Object-2-Record-Java-Binding.html

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top