我尝试使用java从orientdb获取所有数据。我的代码是
public static ArrayList<dbentity.DoctorEntity> viewAllDoctor(){
graph = factory.getTx();
ArrayList<dbentity.DoctorEntity> al=new ArrayList<dbentity.DoctorEntity>();
for (Vertex v : graph.getVerticesOfClass("DoctorA")) {
System.out.println(v.getProperty("@rid"));
dbentity.DoctorEntity disent=new DoctorEntity(v.getProperty("@rid"), v.getProperty("NAME"),v.getProperty("specialization"));
al.add(disent);
}
graph.shutdown();
return al;
}
我想将 @rid 添加到 ArrayList 但它显示错误。我认为数据类型有问题我的 DoctorEntity 类是。
public class DoctorEntity {
private String name;
private String specialization;
private String rid;
public DoctorEntity(Object rid, Object name,Object specialization){
this.name=(String) name;
this.specialization=(String) specialization;
this.rid=(String) rid;
}
public String getName() {
return name;
}
public String getSpecialization() {
return specialization;
}
public String getRid() {
return rid;
}}
错误显示为异常:“java.lang.ClassCastException”消息:“com.tinkerpop.blueprints.impls.orient.OrientVertex 无法转换为 java.lang.String”
您可以将 Vertex 转换为 OrientVertex 并调用 getIdentity().toString()。请参阅here .
for (Vertex v : graph.getVerticesOfClass("DoctorA")) {
OrientVertex vertex = (OrientVertex) v;
dbentity.DoctorEntity disent = new DoctorEntity(vertex.getIdentity().toString(), v.getProperty("NAME"), v.getProperty("specialization"));
al.add(disent);
}
我是一名优秀的程序员,十分优秀!