我将Object API与OrientDB结合使用,并且需要更改我的字段之一。我目前有类似的东西:

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


而且,如果我将数据库中的类型设置为“嵌入”,则会导致错误:

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


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

那么,如何才能最好地解决此问题?

最佳答案

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

课本

public class Book {

private List<String> authors;

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

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


}

班级主要

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:
对象“ Book”已创建,其字段“ authors”为Embeddedlist。 (自动创建)
java - OrientDB使用对象API一对多-LMLPHP

10-08 02:35