spring-data-mongodb
バージョンを使用して1.1.1.RELEASE
います。すべてのメンバー変数がプリミティブ型である場合、引数の名前がおよび注釈 @PersistenceConstructor
を使用してメンバー変数の名前と正確に一致しなくても、オブジェクトを永続化できます。@Field("foo")
@Value("#root.foo")
ただし、MappingInstantiationException
プリミティブ配列を含むオブジェクトを永続化しようとすると、(コンストラクターの不正な引数) が返されます。これは、プリミティブ配列にプリミティブまたはオブジェクトが含まれているかどうかに関係なく当てはまります。これはバグですか、spring-data-mongodb
それとも何か間違っていますか?
junit
およびに依存する以下の 3 つの単体テストを含めましたspring-data-mongodb
。テストでは、次の 3 つのことが示されます。
- オブジェクトのコレクションを含むオブジェクトの永続化は期待どおりに機能します
- プリミティブの配列を含むオブジェクトの永続化が期待どおりに機能しない
- オブジェクトの配列を含むオブジェクトの永続化が期待どおりに機能しない
.
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();
}
}
}
私が取得した例外のスタック トレースも以下に示します。
org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [com.recorder.SpringSourceTest$ArrayContainer]: Illegal arguments for constructor; nested exception is java.lang.IllegalArgumentException: argument type mismatch
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:77)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:229)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:209)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:173)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:169)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:72)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:1820)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1542)
at org.springframework.data.mongodb.core.MongoTemplate.findAll(MongoTemplate.java:1064)
at com.recorder.SpringSourceTest.testObject(SpringSourceTest.java:82)
at com.recorder.SpringSourceTest.testArrayContainer(SpringSourceTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.recorder.SpringSourceTest$ArrayContainer]: Illegal arguments for constructor; nested exception is java.lang.IllegalArgumentException: argument type mismatch
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:158)
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:75)
... 35 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 36 more