英文:
Using generics with Morphia & MongoDB
问题
Short Description:
我在将来自Mongo DB的文档读取到具有泛型属性的Java类中遇到了问题。错误显示为java.lang.Double无法转换为com.mongodb.DBObject
。
Long Description:
我有一个名为RandomDataCollection
的Java类/MongoDB集合,它具有类型为MyGenericProperty<T>
的属性。
@Getter @Setter @NoArgsConstructor @Entity("randomDataCollection")
public class RandomDataCollection {
@Id
private ObjectId id = new ObjectId();
private String name;
@Embedded
private MyGenericProperty<Float> temperatureMumbai;
}
MyGenericProperty<T>
的结构如下:
@Getter @Setter @NoArgsConstructor
public class MyGenericProperty<T> {
private T value;
private String unit;
}
写入数据库正常工作:
MyGenericProperty<Float> temperatureMumbai = new MyGenericProperty<Float>();
temperatureMumbai.setValue(32.8f);
temperatureMumbai.setUnit("°C");
RandomDataCollection randomData = new RandomDataCollection();
randomData.setName("RandomData0711");
randomData.setTemperatureMumbai(temperatureMumbai);
Datastore datastore = getDatastore();
datastore.save(randomData);
但是当我尝试从数据库中读取时:
randomData = datastore.find(RandomDataCollection.class).filter("name =", "RandomData0711").get();
我得到以下异常:
Caused by: java.lang.ClassCastException: java.lang.Double cannot be cast to com.mongodb.DBObject
我找到了一些关于泛型和Morphia的讨论,但老实说我不知道如何将它们应用于我的问题。
我正在使用Morphia 1.5.8。
英文:
Short Description:
I'm having trouble with reading a document from mongo DB into a java class with a generic property. The error says java.lang.Double cannot be cast to com.mongodb.DBObject
Long Description:
I have a java class / mongoDb-Collection called RandomDataCollection
which has properties of type MyGenericProperty<T>
@Getter @Setter @NoArgsConstructor @Entity("randomDataCollection")
public class RandomDataCollection {
@Id
private ObjectId id = new ObjectId();
private String name;
@Embedded
private MyGenericProperty<Float> temperatureMumbai;
}
MyGenericProperty<T>
looks like this
@Getter @Setter @NoArgsConstructor
public class MyGenericProperty<T> {
private T value;
private String unit;
}
Writing to the DB works as expected...
MyGenericProperty<Float> temperatureMumbai = new MyGenericProperty<Float>();
temperatureMumbai.setValue(32.8f);
temperatureMumbai.setUnit("°C");
RandomDataCollection randomData = new RandomDataCollection();
randomData.setName("RandomData0711");
randomData.setTemperatureMumbai(temperatureMumbai);
Datastore datastore = getDatastore();
datastore.save(randomData);
...but when I try to read from the DB...
randomData = datastore.find(RandomDataCollection.class).filter("name =", "RandomData0711").get();
...I get the following exception:
Caused by: java.lang.ClassCastException: java.lang.Double cannot be cast to com.mongodb.DBObject
I did find some discussions about generics and morphia but to be honest I didn't know how to apply those to my issue.
I'm running Morphia 1.5.8
答案1
得分: 1
代码部分不要翻译,只返回翻译好的部分:
几乎肯定有更好的方法来做这个,但这对我有效。
DBCursor cursor = datastore.find(RandomDataCollection.class).filter("name =", name).fetch().getCursor();
List<RandomDataCollection> list = new ArrayList<>();
while (cursor.hasNext()) {
DBObject theObj = cursor.next();
RandomDataCollection rec = new RandomDataCollection();
rec.setId((ObjectId) theObj.get("_id"));
MyGenericProperty<Float> temperatureMumbai = new MyGenericProperty<Float>();
BasicDBObject x = (BasicDBObject) theObj.get("temperatureMumbai");
temperatureMumbai.setValue(x.getFloat("value"));
temperatureMumbai.setUnit(x.getString("unit"));
rec.setTemperatureMumbai(temperatureMumbai);
list.add(rec);
}
英文:
There is almost certainly a better way to do this but this worked for me.
DBCursor cursor = datastore.find(RandomDataCollection.class).filter("name =", name).fetch().getCursor();
List<RandomDataCollection> list = new ArrayList<>();
while (cursor.hasNext()) {
DBObject theObj = cursor.next();
RandomDataCollection rec = new RandomDataCollection();
rec.setId((ObjectId)theObj.get("_id"));
MyGenericProperty<Float> temperatureMumbai = new MyGenericProperty<Float>();
BasicDBObject x = (BasicDBObject)theObj.get("temperatureMumbai");
temperatureMumbai.setValue(x.getFloat("value"));
temperatureMumbai.setUnit(x.getString("unit"));
rec.setTemperatureMumbai(temperatureMumbai);
list.add(rec);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论