OrientDB 一对多使用对象 API
OrientDB one to many using Object API
我在 OrientDB 中使用对象 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
而且,如果我将数据库中的类型设为 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;
}
}
CLASS 主要
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' 作为嵌入列表。 (自动创建)
我在 OrientDB 中使用对象 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
而且,如果我将数据库中的类型设为 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;
}
}
CLASS 主要
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' 作为嵌入列表。 (自动创建)