java - OrientDB 使用对象 API 一对多

标签 java orientdb

我正在使用 OrientDB 的对象 API,并且我需要更改我的字段之一。我目前有类似的东西:

class Book { String author }

但我想将其更改为:

class Book { List<String> authors }

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

我尝试了后者,结果是:

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

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

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

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

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

最佳答案

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

类(class)书

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();
    }
}

来自工作室: 创建了对象“Book”,并且它的字段“authors”作为嵌入列表。 (自动创建) enter image description here

关于java - OrientDB 使用对象 API 一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35452701/

相关文章:

java - 在 JUnit4 中使用@Override

java - JButton 数组上的 ActionListener

javascript - orientjs:在事务中更新插入边

将 2M 记录导入 OrientDB 后 Java OutOfMemoryError/OrientDB WAL Flush Task 错误

java - 在orientDB中,以下数据类型可以在orientDb的图形数据库中使用还是只能在orientDb的文档数据库中使用?

java - 允许用户上传文件

java - 返回类型与继承方法不兼容

java - JNI 调用返回对象的 Java 函数导致 NoSuchMethodError

java - OrientGraph 中的 OConcurrentModificationException

orientdb - 我们如何在 Orientdb 中使用 Tinkerpop 蓝图?