我正在使用spring-data-mongodb
版本1.1.1.RELEASE
。如果所有成员变量都是基本类型,即使@PersistenceConstructor
参数的名称与成员变量的名称不完全匹配,我也可以使用@Field("foo")
和{{ 1}}注释。
但是,当我尝试持久化包含基本数组的对象时,我得到@Value("#root.foo")
(构造函数的非法参数)。无论原始数组是包含基元还是对象,这都适用。这是MappingInstantiationException
中的错误还是我做错了什么?
我在下面包含了三个单元测试,这取决于spring-data-mongodb
和junit
。测试显示了三件事:
spring-data-mongodb
我得到的异常的堆栈跟踪也列在下面。
public class SpringSourceTest {
private static final String DATABASE_HOST = "localhost";
private static final String DATABASE_NAME = "testdb";
private static Mongo s_mongo = null;
private static MongoOperations s_mongoOperations;
@BeforeClass
public static void setUpBeforeClass() {
try {
s_mongo = new Mongo(DATABASE_HOST);
}
catch (UnknownHostException e) {
e.printStackTrace();
}
assertNotNull(s_mongo);
s_mongoOperations = new MongoTemplate(s_mongo, DATABASE_NAME);
assertNotNull(s_mongoOperations);
}
@AfterClass
public static void tearDownAfterClass() {
s_mongo.dropDatabase(DATABASE_HOST);
s_mongo.close();
}
@Test
public void testCollectionContainer() {
System.out.println("---------------testCollectionContainer-----------------");
List<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
CollectionContainer collectionContainer = new CollectionContainer(intList);
testObject(collectionContainer);
}
@Test
public void testArrayContainer() {
int[] properties = new int[]{100, 50};
System.out.println("----------------testArrayContainer----------------");
ArrayContainer arrayContainer = new ArrayContainer(properties);
testObject(arrayContainer);
}
@Test
public void testArrayObjectContainer() {
System.out.println("----------------testArrayObjectContainer----------------");
Integer[] properties = new Integer[]{new Integer(100), new Integer(50)};
ArrayObjectContainer arrayObjectContainer = new ArrayObjectContainer(properties);
testObject(arrayObjectContainer);
}
private void testObject(Object a_object) {
assertNotNull(a_object);
System.out.println("OLD OBJECT: " + a_object);
Class<?> objectClass = a_object.getClass();
s_mongoOperations.dropCollection(objectClass);
s_mongoOperations.createCollection(objectClass);
s_mongoOperations.insert(a_object);
List<?> createdObjects = null;
try {
createdObjects = s_mongoOperations.findAll(objectClass);
for (Object createdObject : createdObjects) {
System.out.println("NEW OBJECT: " + createdObject);
assertNotNull(createdObject);
assertEquals(a_object, createdObject);
}
}
catch (Exception e){
System.out.println("FAILED TO RETRIEVE: " + objectClass);
System.out.println(e.getMessage().toString());
fail();
}
s_mongoOperations.dropCollection(objectClass);
}
@Document
public static class CollectionContainer {
@Field("property") private final List<Integer> m_property;
@PersistenceConstructor
public CollectionContainer(@Value("#root.property") List<Integer> a_property) {
validateNotNull(a_property);
m_property = a_property;
}
private void validateNotNull(List<Integer> a_property) {
assertNotNull(a_property);
}
public List<Integer> property() {
return m_property;
}
@Override
public boolean equals(Object a_obj) {
if (this == a_obj) {
return true;
}
if ((a_obj == null) || getClass() != a_obj.getClass()) {
return false;
}
final CollectionContainer other = (CollectionContainer) a_obj;
for (int i = 0; i < m_property.size(); i++) {
if (!m_property.get(i).equals(other.property().get(i))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + m_property.hashCode();
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("CollectionContainer={")
.append("m_property=").append(m_property)
.append("}").toString();
return sb.toString();
}
}
@Document
public static class ArrayContainer {
@Field("property") private final int[] m_property;
@PersistenceConstructor
public ArrayContainer(@Value("#root.property") int[] a_property) {
validateNotNull(a_property);
m_property = a_property;
}
private void validateNotNull(int[] a_property) {
assertNotNull(a_property);
}
public int[] property() {
return m_property;
}
@Override
public boolean equals(Object a_obj) {
if (this == a_obj) {
return true;
}
if ((a_obj == null) || getClass() != a_obj.getClass()) {
return false;
}
final ArrayContainer other = (ArrayContainer) a_obj;
for (int i = 0; i < m_property.length; i++) {
if (m_property[i] != other.property()[i]) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + m_property.hashCode();
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("ArrayContainer={");
sb.append("m_property=[");
for (int i = 0; i < m_property.length; i++) {
sb.append(m_property[i]).append(",");
}
sb.append("]}").toString();
return sb.toString();
}
}
@Document
public static class ArrayObjectContainer {
@Field("property") private final Integer[] m_property;
@PersistenceConstructor
public ArrayObjectContainer(@Value("#root.property") Integer[] a_property) {
validateNotNull(a_property);
m_property = a_property;
}
private void validateNotNull(Integer[] a_property) {
assertNotNull(a_property);
}
public Integer[] property() {
return m_property;
}
@Override
public boolean equals(Object a_obj) {
if (this == a_obj) {
return true;
}
if ((a_obj == null) || getClass() != a_obj.getClass()) {
return false;
}
final ArrayObjectContainer other = (ArrayObjectContainer) a_obj;
for (int i = 0; i < m_property.length; i++) {
if (!m_property[i].equals(other.property()[i])) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + m_property.hashCode();
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("ArrayObjectContainer={");
sb.append("m_property=[");
for (int i = 0; i < m_property.length; i++) {
sb.append(m_property[i]).append(",");
}
sb.append("]}").toString();
return sb.toString();
}
}
}