使用@DBRef注释进行引用时,MongoDB异常的Spring-Data

时间:2012-01-10 10:04:40

标签: java mongodb spring-data

这里我尝试使用@DBRef注释来引用来自Spring-Data for MongoDB中另一个的对象。但是在运行MongoApp类之后,结果表数据将被插入到DB中,但是它显示没有找到Person对象插入的方法异常,如下例所示。这是怎么回事?

public class Result {

  @Id
  @NotNull
  private String subjectId;

  @NotNull
  private String subName;

  @NotNull
  @Max(value = 100)
  private int marks;

  public Result(String subjectId, String subName, int marks) {
    this.subjectId = subjectId;
    this.subName = subName;
    this.marks = marks;
  }
}

public class Person {


      @Id
      @NotNull
      private String abc;

      @NotNull
      @Size(max = 8, min = 4, message = "Not a Valid Name")
      private String name;

      @Max(value = 100)
      private int age;

      @DBRef(collection = "result")
      private Result result;

      public Person(String id, String name, int age, Result result) {
        this.abc = id;
        this.name = name;
        this.age = age;
        this.result = result;
      }
}

public static void main(String[] args) throws Exception{
  String location = "com/springMongo/core/applicationContext-springdata.xml";
  ApplicationContext context = new ClassPathXmlApplicationContext(location);
  MongoOperations mongoOps = (MongoOperations) context.getBean("mongoTemplate");
  mongoOps.insert(new Result("1","Math",50));

  Query query = new Query(Criteria.where("subjectId").is("1"));
  mongoOps.insert(new Person("1212", "Ravi", 34, mongoOps.findOne(query, Result.class)));
}

在上面的应用程序运行后,显示以下异常

Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.data.mongodb.core.mapping.MongoPersistentProperty.isCollection()Z
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:448)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$5.doWithAssociation(MappingMongoConverter.java:433)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:185)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:420)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:351)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:316)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:77)
at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:592)
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:554)
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:545)
at com.springMongo.core.MongoApp.main(MongoApp.java:43)

3 个答案:

答案 0 :(得分:0)

这听起来像版本控制的jar依赖问题。

确保您拥有与正确版本的弹簧数据罐匹配的正确版本的mongodb驱动程序jar。

答案 1 :(得分:0)

这似乎有点奇怪,因为在原始调用结果的同一个JAR中为类抛出了NoSuchMethodException。你介意尝试下面这个吗?

  • 确保您的类路径,输出文件夹等中没有spring-data-mongodb-${version}.jar的多个版本。
  • 尝试使用最新的1.0.0.RELEASE版本的Spring Data MongoDB
  • 确保您在Spring Data MongoDB的pom.xml和参考文档中所述的匹配版本中拥有Spring Data MongoDB和Spring Data Commons

答案 2 :(得分:0)

我遇到了同样的问题。就我而言,升级我的依赖项解决了这个问题。

<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-mongodb</artifactId>
<!--   <version>1.0.0.M5</version> -->
   <version>1.0.1.RELEASE</version>
</dependency>