使用 Morphia 的 ClassCastException
ClassCastException using Morphia
我正在尝试获取 MongoDB 的集合并使用 Morphia 将记录转换为 javabean,但是当我尝试获取对象集合时(请参阅下面的 App 代码),出现转换错误:
Exception in thread "main" java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.homework.Score
文档
{
"_id" : 19,
"name" : "Student 01",
"scores" : [
{
"type" : "exam",
"score" : 44.51211101958831
},
{
"type" : "quiz",
"score" : 0.6578497966368002
},
{
"type" : "homework",
"score" : 93.36341655949683
},
{
"type" : "homework",
"score" : 49.43132782777443
}
]
}
学生
@Entity("students")
public class Student {
@Id
private int id;
@Property("name")
private String name;
@Property("scores")
private List<Score> scores;
gets and sets
}
得分
@Embedded
public class Score {
@Property("type")
private String type;
@Property("score")
private double score;
gets and sets
}
应用程序
private static MongoClient client = new MongoClient();
private static final Morphia morphia = new Morphia();
private static Datastore datastore;
private static Query<Student> query;
public static void main(String[] args) {
morphia.mapPackage("com.homework");
datastore = morphia.createDatastore(client, "school");
datastore.ensureIndexes();
query = datastore.createQuery(Student.class);
List<Student> students = query.asList();
List<Score> scoresCurr;
Score score1;
Score score2;
int idx;
for (Student s : students) {
scoresCurr = s.getScores();
score1 = scoresCurr.get(2); <<<< exception occurs here
score2 = scoresCurr.get(3);
idx = score1.getScore() < score2.getScore() ? 2 : 3;
scoresCurr.remove(idx);
s.setScores(scoresCurr);
datastore.save(s);
}
client.close();
}
这是其他人也经历过的类似行为。
Can't find a codec for class , Morphia , Spring.
如果您认为这是不正确的行为,可以在此处提交错误报告。
https://github.com/mongodb/morphia/issues
好的,现在进入解决方案。您可以通过两种方式修复它。
解决方案 1
您可以从 score
pojo 和
中删除 @Embedded
注释
替换
@Property("scores")
private List<Score> scores;
和
@Embedded("scores")
private List<Score> scores;
解决方案 2
删除 Student
pojo 中 scores
字段的 @property
注释。
我正在尝试获取 MongoDB 的集合并使用 Morphia 将记录转换为 javabean,但是当我尝试获取对象集合时(请参阅下面的 App 代码),出现转换错误:
Exception in thread "main" java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.homework.Score
文档
{
"_id" : 19,
"name" : "Student 01",
"scores" : [
{
"type" : "exam",
"score" : 44.51211101958831
},
{
"type" : "quiz",
"score" : 0.6578497966368002
},
{
"type" : "homework",
"score" : 93.36341655949683
},
{
"type" : "homework",
"score" : 49.43132782777443
}
]
}
学生
@Entity("students")
public class Student {
@Id
private int id;
@Property("name")
private String name;
@Property("scores")
private List<Score> scores;
gets and sets
}
得分
@Embedded
public class Score {
@Property("type")
private String type;
@Property("score")
private double score;
gets and sets
}
应用程序
private static MongoClient client = new MongoClient();
private static final Morphia morphia = new Morphia();
private static Datastore datastore;
private static Query<Student> query;
public static void main(String[] args) {
morphia.mapPackage("com.homework");
datastore = morphia.createDatastore(client, "school");
datastore.ensureIndexes();
query = datastore.createQuery(Student.class);
List<Student> students = query.asList();
List<Score> scoresCurr;
Score score1;
Score score2;
int idx;
for (Student s : students) {
scoresCurr = s.getScores();
score1 = scoresCurr.get(2); <<<< exception occurs here
score2 = scoresCurr.get(3);
idx = score1.getScore() < score2.getScore() ? 2 : 3;
scoresCurr.remove(idx);
s.setScores(scoresCurr);
datastore.save(s);
}
client.close();
}
这是其他人也经历过的类似行为。
Can't find a codec for class , Morphia , Spring.
如果您认为这是不正确的行为,可以在此处提交错误报告。
https://github.com/mongodb/morphia/issues
好的,现在进入解决方案。您可以通过两种方式修复它。
解决方案 1
您可以从 score
pojo 和
@Embedded
注释
替换
@Property("scores")
private List<Score> scores;
和
@Embedded("scores")
private List<Score> scores;
解决方案 2
删除 Student
pojo 中 scores
字段的 @property
注释。