这是我的收藏:
db.power.find().pretty()
{
"_id" : ObjectId("513e4022cc6d8d7ff2c83239"),
"Indicator" : "One",
"sex" : "male"
}
如何逃脱ClassCastException
?
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
public class Test {
public static void main(String args[]) throws UnknownHostException {
Mongo mongo = new Mongo();
DB db = mongo.getDB("at");
DBCollection testdata = db.getCollection("power");
BasicDBObject query = new BasicDBObject();
query.put("Indicator", "One");
PowerData data = (PowerData) testdata.findOne(query);
System.out.println(data.getSize());
}
}
import com.mongodb.BasicDBObject;
public class PowerData extends BasicDBObject{
public String getSize() {
return (String) get("sex");
}
public void setSize(String sex) {
put("sex", sex);
}
public String getIndicator() {
return (String) get("Indicator");
}
public void setIndicator(String Indicator) {
put("Indicator", Indicator);
}
public String getId() {
return (String) get("_id");
}
public void setId(String _id) {
put("_id", _id);
}
}
线程“main”java.lang.ClassCastException 中的异常:com.mongodb.BasicDBObject 无法在 Test.main 中转换为 PowerData(Test.java:19)