Dear Oliver,
Here is a working single-java-file example that demonstrates the problem :
Code:
package kam.albert.lab.convert;
import java.util.ArrayList;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Component;
@Component
@Scope(value="singleton")
public class TestBug {
@Autowired private MongoOperations ops;
private static String collectionName = "test";
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"test-context.xml"
);
TestBug bean = ctx.getBean(TestBug.class);
bean.test();
}
private void test() {
if (!this.ops.collectionExists(collectionName)) {
this.ops.createCollection(collectionName);
}
MyDomain domain = new MyDomain();
// COMMENT THIS LINE TO MAKE THE EXCEPTION SHOWS UP
domain.names.add("hello");
this.ops.insert(domain);
MyDomain dbDomain = this.ops.findById(domain.id, MyDomain.class);
List<String> values = dbDomain.names.getValues();
System.out.println("oink : " + values.getClass() + " : " + values);
}
private static class MyDomain {
private ObjectId id; // just for lookup later after insertion
private MyList<String> names;
MyDomain() {
this.id = new ObjectId();
this.names = new MyList<String>();
}
}
private static class MyList<T> extends MyPropertyBase<List<T>> {
MyList() {
this.value = new ArrayList<>();
}
void add(T elem) {
this.value.add(elem);
}
public List<T> getValues() {
return this.value;
}
}
private static class MyPropertyBase<T> {
T value;
}
}
The above is a working example with the output of :
Code:
oink : class java.util.ArrayList : [hello]
One just need to comment the line :
Code:
domain.names.add("hello");
to make the exception shows up :
Code:
Exception in thread "main" java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.List
at kam.albert.lab.convert.TestBug$MyList.getValues(TestBug.java:65)
at kam.albert.lab.convert.TestBug.test(TestBug.java:44)
at kam.albert.lab.convert.TestBug.main(TestBug.java:30)
Please note that this doesnt happen in 1.0.4 version.
Thank you !