MongoDB 存储库 - 如何注释子 class 不是文档?
MongoDB repository - how to annotate child class which is not document?
我有 2 个 classes :
@Document(collection="student")
public class MongoStudent {
@Id
private BigInteger id;
@Field("name")
private String name;
@Field("lname")
private String lname;
@Field("studium")
private List<MongoStudium> studium;
@PersistenceConstructor
public MongoStudent(BigInteger id,String name,String lname,List<MongoStudium> studium){
this.id = id;
this.name = name;
this.lname = lname
this.studium = studium;
}
...etc, getters,setters
public class MongoStudium {
@Field
private BigInteger id;
@Field
private Date start;
@Field
private Date end;
@Field("course")
private String course;
@PersistenceConstructor
public MongoStudium(BigInteger id, Date start, Date end, String course) {
this.id = id;
this.start = start;
this.end = end;
this.course = course;
}
...etc, getters,setters
第一个 class 是文档,第二个 class 不是文档。它给我这样的 mongodocument :
{
"_id": "1006262",
"name": "Kugujalu",
"lname": "Najahalovu",
"studium": [
{
"_id": "992",
"start": {
"$date": "1998-08-31T22:00:00.000Z"
},
"end": {
"$date": "2003-06-10T22:00:00.000Z"
},
"course": "MCH",
}
],
"_class": "mongodb.MongoStudent"
}
我想要这种形式的文档,因为我很容易为这个文档编写查询。问题是 mongodb 存储库无法从该文档读取数据。而且我不明白为什么,因为保存数据没有问题。它给我这样的错误:
Exception in thread "main" org.springframework.data.mapping.MappingException: No property studium found on entity class mongodb.MongoStudium to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:68)
at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:49)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.extractInvocationArguments(ClassGeneratingEntityInstantiator.java:248)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:221)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:86)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:273)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:253)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:978)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1386)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1335)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:335)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:297)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:253)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:202)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:198)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:86)
at org.springframework.data.mongodb.core.MongoTemplate$ProjectingReadCallback.doWith(MongoTemplate.java:2827)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2446)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2265)
at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:213)
at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.all(ExecutableFindOperationSupport.java:169)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.lambda$getExecution(AbstractMongoQuery.java:113)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:97)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy37.findByLname(Unknown Source)
at sk.upjs.nosql.mongodb.StudentService.writeNames(StudentService.java:39)
at sk.upjs.nosql.mongodb.ConnectToDB.main(ConnectToDB.java:37)
看起来 mongodb 存储库遗漏了一些注释。但是哪里 ?有帮助吗?
尝试更改字段名称
private List<MongoStudium> studium;
至
private List<MongoStudium> mongoStudium;
和
的构造函数
public MongoStudent(BigInteger id,String name,String lname,List<MongoStudium> mongoStudium){
可能您在 MongoStudium 中删除了 属性 'studium',但数据库中存在 属性。所以 Spring 尝试映射到一个没有这个字段的对象中。
您必须删除数据库中 MongoStudium 的所有记录中的字段 'studium' 或清除数据库中的整个集合。
我有 2 个 classes :
@Document(collection="student")
public class MongoStudent {
@Id
private BigInteger id;
@Field("name")
private String name;
@Field("lname")
private String lname;
@Field("studium")
private List<MongoStudium> studium;
@PersistenceConstructor
public MongoStudent(BigInteger id,String name,String lname,List<MongoStudium> studium){
this.id = id;
this.name = name;
this.lname = lname
this.studium = studium;
}
...etc, getters,setters
public class MongoStudium {
@Field
private BigInteger id;
@Field
private Date start;
@Field
private Date end;
@Field("course")
private String course;
@PersistenceConstructor
public MongoStudium(BigInteger id, Date start, Date end, String course) {
this.id = id;
this.start = start;
this.end = end;
this.course = course;
}
...etc, getters,setters
第一个 class 是文档,第二个 class 不是文档。它给我这样的 mongodocument :
{
"_id": "1006262",
"name": "Kugujalu",
"lname": "Najahalovu",
"studium": [
{
"_id": "992",
"start": {
"$date": "1998-08-31T22:00:00.000Z"
},
"end": {
"$date": "2003-06-10T22:00:00.000Z"
},
"course": "MCH",
}
],
"_class": "mongodb.MongoStudent"
}
我想要这种形式的文档,因为我很容易为这个文档编写查询。问题是 mongodb 存储库无法从该文档读取数据。而且我不明白为什么,因为保存数据没有问题。它给我这样的错误:
Exception in thread "main" org.springframework.data.mapping.MappingException: No property studium found on entity class mongodb.MongoStudium to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:68)
at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:49)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.extractInvocationArguments(ClassGeneratingEntityInstantiator.java:248)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:221)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:86)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:273)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:253)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:978)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1386)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1335)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:335)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:297)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:253)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:202)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:198)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:86)
at org.springframework.data.mongodb.core.MongoTemplate$ProjectingReadCallback.doWith(MongoTemplate.java:2827)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2446)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2265)
at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:213)
at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.all(ExecutableFindOperationSupport.java:169)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.lambda$getExecution(AbstractMongoQuery.java:113)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:97)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy37.findByLname(Unknown Source)
at sk.upjs.nosql.mongodb.StudentService.writeNames(StudentService.java:39)
at sk.upjs.nosql.mongodb.ConnectToDB.main(ConnectToDB.java:37)
看起来 mongodb 存储库遗漏了一些注释。但是哪里 ?有帮助吗?
尝试更改字段名称
private List<MongoStudium> studium;
至
private List<MongoStudium> mongoStudium;
和
的构造函数public MongoStudent(BigInteger id,String name,String lname,List<MongoStudium> mongoStudium){
可能您在 MongoStudium 中删除了 属性 'studium',但数据库中存在 属性。所以 Spring 尝试映射到一个没有这个字段的对象中。
您必须删除数据库中 MongoStudium 的所有记录中的字段 'studium' 或清除数据库中的整个集合。