使用Morphia和MongoDB的泛型功能

huangapple go评论85阅读模式
英文:

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&lt;T&gt;

@Getter @Setter @NoArgsConstructor @Entity(&quot;randomDataCollection&quot;)
public class RandomDataCollection {

	@Id
    private ObjectId id = new ObjectId();

    private String name;

    @Embedded
    private MyGenericProperty&lt;Float&gt; temperatureMumbai;

}

MyGenericProperty&lt;T&gt; looks like this

@Getter @Setter @NoArgsConstructor
public class MyGenericProperty&lt;T&gt; {

	private T value;

	private String unit;

}

Writing to the DB works as expected...

MyGenericProperty&lt;Float&gt; temperatureMumbai = new MyGenericProperty&lt;Float&gt;();
temperatureMumbai.setValue(32.8f);
temperatureMumbai.setUnit(&quot;&#176;C&quot;);

RandomDataCollection randomData = new RandomDataCollection();
randomData.setName(&quot;RandomData0711&quot;);
randomData.setTemperatureMumbai(temperatureMumbai);

Datastore datastore = getDatastore();
datastore.save(randomData);

...but when I try to read from the DB...

randomData = datastore.find(RandomDataCollection.class).filter(&quot;name =&quot;, &quot;RandomData0711&quot;).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(&quot;name =&quot;, name).fetch().getCursor();
List&lt;RandomDataCollection&gt; list = new ArrayList&lt;&gt;();
while (cursor.hasNext()) {
	DBObject theObj = cursor.next();
	RandomDataCollection rec = new RandomDataCollection();
	rec.setId((ObjectId)theObj.get(&quot;_id&quot;));
    MyGenericProperty&lt;Float&gt; temperatureMumbai = new MyGenericProperty&lt;Float&gt;();
    BasicDBObject x = (BasicDBObject)theObj.get(&quot;temperatureMumbai&quot;);
    temperatureMumbai.setValue(x.getFloat(&quot;value&quot;));
    temperatureMumbai.setUnit(x.getString(&quot;unit&quot;));
    rec.setTemperatureMumbai(temperatureMumbai);
    list.add(rec);
}

huangapple
  • 本文由 发表于 2020年4月8日 18:25:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61098460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定