OrientDB一对多使用Object API

时间:2016-02-17 09:31:17

标签: java orientdb

我正在使用ObjectDB的Object API,我需要更改其中一个字段。我目前有类似的东西:

class Book { String author }

但我想将其更改为:

class Book { List<String> authors }

我的问题是:如何在OrientDB中保留这个字符串列表?我是否必须将列表声明为@Embedded?我是否必须将架构定义为LINKLIST?

我尝试了后者,结果是:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.orientechnologies.orient.core.db.record.OIdentifiable

而且,如果我将数据库中的类型设为Embbed,则会导致错误:

Caused by: java.lang.IllegalArgumentException: Type EMBEDDED must be a multi value type (collection or map)

不幸的是,它没有提供太多信息。

那么,我怎样才能最好地解决这个问题呢?

1 个答案:

答案 0 :(得分:2)

起点: 我从Java API创建了一个新的数据库,并保存了一个作者列表。

CLASS BOOK

public class Book {

private List<String> authors;

public void setAuthors (List<String> pAuthors){
    this.authors = pAuthors;
}

public List<String> getAuthors(){
    return this.authors;
}

}

CLASS MAIN

public class DatabaseTipoObject {

private static String remote="remote:localhost/";

public static void main(String[] args) {
    String nomeDb="Object";
    String path=remote+nomeDb;

    try {
        OServerAdmin serverAdmin = new OServerAdmin(path).connect("root", "root");
        if(serverAdmin.existsDatabase()){   
            System.out.println("Database '"+nomeDb +"' exist..");
        }
        else{ 
            serverAdmin.createDatabase(nomeDb, "object", "plocal");
            System.out.println(" Database '"+nomeDb +"' created!..");
        }

        OObjectDatabaseTx db = new OObjectDatabaseTx (path);
        db.open("root","root");

        db.getEntityManager().registerEntityClass(Book.class);
        Book book = db.newInstance(Book.class);
        List<String> myAuthors = new ArrayList();
        myAuthors.add("Archimede");
        myAuthors.add("Pitagora");
        book.setAuthors(myAuthors);

        db.save(book);
        System.out.println("Data inserted!" );

        //get info by query
        for (Book book_retrive : db.browseClass(Book.class)) {
            System.out.println("#: " +book_retrive.getAuthors().get(0) );
            System.out.println("#: " +book_retrive.getAuthors().get(1) );
        }

        db.close();

        serverAdmin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

来自Studio: 对象&#39; Book&#39;创建了它,并且它有作者&#39;作家&#39;作为嵌入式列表。 (自动创建) enter image description here