-3

This is the error which I'm getting java.lang.ClassCastException: java.lang.Integer cannot be cast to com.mongodb.DBObject

Here's my POJO

@Entity("cars")
@Getter
@Setter
public class Car {
  @Id
  private ObjectId id;    

  private String defaultChoice;    

  private Object input;
}

I was able to save this in the cars collection in mongo using morphia with json as

{
  "defaultChoice": "sampleChoice",
  "input": 432
}

In mongo, the field input is saved with type int32

The exception happens when I try to retrieve the data using morphia. It is not able to map/deserialize the field with Object as type. I tried by changing it to Integer input. And it worked. I want to try keep this field as Object if possible

Is there a way to fix this ? May be write a custom mapper ?

Prozac
  • 25
  • 1
  • 3

1 Answers1

0

MongoDB best practice is to use the same field type for the same field in a collection. So Morphia has been designed to assume this approach. As the others have said, you should use Integer for this field, but note that you can embed other java classes as subdocuments with Morphia.

Nic Cottrell
  • 9,401
  • 7
  • 53
  • 76