Java 使用@DBRef注释进行引用时MongoDB的Spring数据异常
在这里,我尝试使用@DBRef注释从Spring Data for MongoDB中的另一个对象引用对象。但在运行MongoApp类之后,结果表数据被插入到数据库中,但其显示的“未找到任何方法”,Person对象插入异常,如下面的示例所示。这里发生了什么事Java 使用@DBRef注释进行引用时MongoDB的Spring数据异常,java,mongodb,spring-data,Java,Mongodb,Spring Data,在这里,我尝试使用@DBRef注释从Spring Data for MongoDB中的另一个对象引用对象。但在运行MongoApp类之后,结果表数据被插入到数据库中,但其显示的“未找到任何方法”,Person对象插入异常,如下面的示例所示。这里发生了什么事 public class Result { @Id @NotNull private String subjectId; @NotNull private String subName; @NotNull @
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)
这听起来像是一个jar依赖问题
确保您拥有与spring数据jar的正确版本相匹配的mongodb驱动程序jar的正确版本。这听起来像是jar依赖问题wrt版本控制
确保您拥有与spring数据jar的正确版本相匹配的mongodb驱动程序jar的正确版本。这似乎有点奇怪,因为原始调用结果是针对同一jar中的类抛出的
NoSuchMethodException
。你介意试试下面的吗
- 确保类路径、输出文件夹等中没有多个版本的
spring数据mongodb-${version}.jar
- 尝试使用Spring Data MongoDB的最新1.0.0.0版本
- 确保Spring Data MongoDB的
和参考文档中所述的匹配版本中有Spring Data MongoDB和Spring Data Commonspom.xml
NoSuchMethodException
。你介意试试下面的吗
- 确保类路径、输出文件夹等中没有多个版本的
spring数据mongodb-${version}.jar
- 尝试使用Spring Data MongoDB的最新1.0.0.0版本
- 确保Spring Data MongoDB的
和参考文档中所述的匹配版本中有Spring Data MongoDB和Spring Data Commonspom.xml
- 我也有同样的问题。在我的例子中,升级依赖项解决了问题
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<!-- <version>1.0.0.M5</version> -->
<version>1.0.1.RELEASE</version>
</dependency>
org.springframework.data
spring数据mongodb
1.0.1.1发布
我也有同样的问题。在我的例子中,升级依赖项解决了问题
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<!-- <version>1.0.0.M5</version> -->
<version>1.0.1.RELEASE</version>
</dependency>
org.springframework.data
spring数据mongodb
1.0.1.1发布
您使用的是哪个版本的spring数据和mongodb驱动程序?mongo java驱动程序2.7.2;spring数据mongodb 1.0.0.M5;弹簧芯3.1.0.1松开;SpringContext 3.1.0.0版本;spring data commons core 1.2.0.Release您使用的是哪个版本的spring data和mongodb驱动程序?mongo java驱动程序2.7.2;spring数据mongodb 1.0.0.M5;弹簧芯3.1.0.1松开;SpringContext 3.1.0.0版本;spring data commons core 1.2.0.Release我正在使用最新的mongo java驱动程序2.7.2和最新的spring data mongodb 1.0.0.M5,但仍然存在问题您是否有任何具有多个版本的重复JAR?我正在使用最新的mongo java驱动程序2.7.2和最新的spring data mongodb 1.0.0.M5,但仍然存在问题您是否有任何重复JAR多个版本?