简体   繁体   English

为什么会出现此异常 - java.lang.ClassCastException:java.lang.String无法强制转换为com.mongodb.DBObject

[英]Why has this exception started occurring - java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject

I am developing my first Java, MongoDB, Morphia application and cannot solve the following exception:- 我正在开发我的第一个Java,MongoDB,Morphia应用程序,无法解决以下异常: -

java.lang.RuntimeException: java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:74)
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:797)
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:250)
    at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:191)
    at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134)
    at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146)
    at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117)
    at org.mongodb.morphia.query.QueryImpl.asList(QueryImpl.java:150)
    at test.DatabaseManagerTest.testListParent(DatabaseManagerTest.java:172)

My Tech Stack is as follows:- 我的技术堆栈如下: -

Java 8 jdk1.8.0_112
Morphia 1.2.1
Mongo java driver 3.2.2

My Parent Entity class:- 我的父母实体课程: -

@Entity("parent")
public class Parent {

    @Id
    private ObjectId id;

    @Indexed(options = @IndexOptions(unique = false))
    private Child child;

    private String comment;

    private Date updateTimestamp;

}

My Child entity:- 我的孩子实体: -

@Embedded
public class Child {

    private int value;
    private String name;
}

The JUNIT code that fails:- 失败的JUNIT代码: -

final Datastore datastore = DatabaseManager.getDatastore();

final Query<Parent> query = datastore.createQuery(Parent.class);
final List<Parent> parents = query.asList(); <<<< EXCEPTION OCCURS HERE

for (Parent parent : parents) {
    Assert.assertNotNull(parent);
}

What mistake have I made in annotating my two entity classes? 我在注释我的两个实体类时犯了什么错误?

I guess its related to the embedded Child class as the stack trace mentions org.mongodb.morphia.mapping.EmbeddedMapper . 我猜它与嵌入的Child类有关,因为堆栈跟踪提到了org.mongodb.morphia.mapping.EmbeddedMapper

What I find strange is that I havent chnaged the Parent and/or Child class in anyway, and all my tests used to pass fine. 我觉得很奇怪的是,无论如何我还没有完成家长和/或儿童课程,而且我所有的测试都过得很好。

Today I re run my tests and they have started throwing this exception. 今天我运行我的测试,他们已经开始抛出这个异常了。

I think the problem is the data stored in child field. 我认为问题是存储在子字段中的数据。 You have some bad data. 你有一些不好的数据。 If I have to guess as I can't look at data you've a child field with data like 如果我不得不猜测,因为我无法查看数据,那么您的子字段就像数据一样

"child" : "somestring" “孩子”:“somestring”

This will explain why you'll receive that specifc type of class cast exception. 这将解释为什么您将收到该特定类型的类强制转换异常。

I encountered the same problem. 我遇到了同样的问题。 I think that mongo does not consider a string datatype as an object. 我认为mongo不会将字符串数据类型视为对象。 If the data stored in mongo is of type string while the associated Java variable is typed as Object then the variable can not be cast and this exception is thrown. 如果存储在mongo中的数据是string类型,而关联的Java变量被键入为Object则无法转换该变量并抛出此异常。

In my case, the issue was that I placed an ArrayList<String> in an Object variable and that morphia inserted it into Mongo as a String (an ArrayList with a single value seems to be interpreted as a single string). 在我的例子中,问题是我在一个Object变量中放置了一个ArrayList<String> ,并且morphia将它作为String插入到Mongo中(具有单个值的ArrayList似乎被解释为单个字符串)。 The causes an exception which prevents the loading of the Java object. 导致异常阻止加载Java对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:org.bson.types.ObjectId无法转换为com.mongodb.DBObject - java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to com.mongodb.DBObject 线程“主”中的异常java.lang.ClassCastException:无法将java.lang.String强制转换为com.mongodb.BasicDBObject - Exception in thread “main” java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.BasicDBObject java.lang.ClassCastException:无法将java.lang.String强制转换为com.security.model.UserRequest - java.lang.ClassCastException: java.lang.String cannot be cast to com.security.model.UserRequest java.lang.ClassCastException:java.lang.String无法强制转换为com.example.service.AlbumService - java.lang.ClassCastException: java.lang.String cannot be cast to com.example.service.AlbumService java.lang.ClassCastException:无法将java.lang.String强制转换为com.tutorial.stateful.Book - java.lang.ClassCastException: java.lang.String cannot be cast to com.tutorial.stateful.Book 嵌套异常是 java.lang.ClassCastException: java.lang.String 不能强制转换 - Nested exception is java.lang.ClassCastException: java.lang.String cannot be cast 为什么显示java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String? - Why show java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String? java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Long - java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long JRException:java.lang.ClassCastException:java.lang.String无法转换为java.lang.Boolean - JRException: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean java.lang.ClassCastException:无法将java.lang.Float强制转换为java.lang.String - java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM