MongoDB存储库 - 如何注释不是文档的子类?

时间:2018-04-26 09:20:47

标签: java mongodb spring-repositories

我有2个班级:

@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

第一类是文档,第二类不是文档。它给了我这样的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$1(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存储库错过了一些注释。但是哪里 ?有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

尝试从

更改字段名称
<textarea id="myArea"></textarea>
<script>
document.getElementById("myArea").addEventListener("keydown",function(event){
    if(event.code==="Tab"){
        var cIndex=this.selectionStart;
        this.value=[this.value.slice(0,cIndex),//Slice at cursor index
            "\t",                              //Add Tab
            this.value.slice(cIndex)].join('');//Join with the end
        event.stopPropagation();
        event.preventDefault();                //Don't quit the area
        this.selectionStart=cIndex+1;
        this.selectionEnd=cIndex+1;            //Keep the cursor in the right index
    }
});
</script>

private List<MongoStudium> studium;

的构造函数
private List<MongoStudium> mongoStudium;

答案 1 :(得分:0)

可能你删除了MongoStudium中的属性'studium'但DB中存在属性。所以Spring尝试映射到一个没有这个字段的对象。

您必须删除db中MongoStudium的所有记录中的字段'studium'或清除db中的整个集合。