java - 使用 Spring MongoTemplate 在 mongo 中插入数据时获取 java.lang.NoSuchMethodError : org. springframework.cglib.core.ReflectUtils.defineClass

标签 java spring mongodb spring-mongodb spring-mongo

我正在尝试使用 Spring MongoTemplate 将一些数据(对象列表)插入到 mongoDB 中,但出现异常:

14:08:04.430 [main] DEBUG org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator - Analyzing class class Child1 for index information.
Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.cglib.core.ReflectUtils.defineClass(Ljava/lang/String;[BLjava/lang/ClassLoader;Ljava/security/ProtectionDomain;Ljava/lang/Class;)Ljava/lang/Class;
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$PropertyAccessorClassGenerator.generateCustomAccessorClass(ClassGeneratingPropertyAccessorFactory.java:324)
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.createAccessorClass(ClassGeneratingPropertyAccessorFactory.java:198)
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.potentiallyCreateAndRegisterPersistentPropertyAccessorClass(ClassGeneratingPropertyAccessorFactory.java:184)
    at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory.getPropertyAccessor(ClassGeneratingPropertyAccessorFactory.java:92)
    at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:455)
    at org.springframework.data.mongodb.core.EntityOperations$AdaptibleMappedEntity.of(EntityOperations.java:592)
    at org.springframework.data.mongodb.core.EntityOperations$AdaptibleMappedEntity.access$100(EntityOperations.java:570)
    at org.springframework.data.mongodb.core.EntityOperations.forEntity(EntityOperations.java:106)
    at org.springframework.data.mongodb.core.MongoTemplate.doInsertBatch(MongoTemplate.java:1321)
    at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1268)

数据实际上是一个使用构建器模式创建的对象,例如:

abstract public class Parent <? extends Parent<?>> {
      private String parentId;
      public String getParentId(){
        return parentId;
      }
      public B setParentId(String parentId){
        this.parentId=parentId;
      }
      abstract B self() {};
}

子类:

public class Child1 extends Parent<Child1> {
      private String childId;
      public String getChildId(){
        return childId;
      }
      public Child1 setChildId(String childId){
        this.childId=childId;
      }
      public Child1 self() {
        return this;
      };
}     

第二个 child 类:

public class Child2 extends Parent<Child2> {
    private String childId;
    private String childValue;

    public String getChildId(){
        return childId;
    }

    public Child2 setChildId(String childId){
        this.childId=childId;
    }

    public String getChildValue(){
        return childValue;
    }

    public Child2 setChildValue(String childValue){
        this.childValue=childValue;
    }

    public Child2 self() {
        return this;
    }
}

MongoUtil 和主类:

public class MongoUtil {

    @Autowired
    MongoTemplate mongoTemplate;

    public static void persistToMongo(List<? extends Parent<?>> listOfObjects, String collectionName) {

        //Here the exception is thrown.
        mongoTemplate.insert(listOfObjects, collectionName);
    }

    public static void main(String[] args) {
        Child1 child1 = new Child1().setChildId("id1").setParentId("parentId1");
        List<Child1> child1List = new ArrayList<>();
        child1List.add(child1);

        MongoUtil.persistToMongo(child1List, "someCollection");

        Child2 child2 = new Child2().setChildId("id2").setParentId("parentId2").setChildValue("val2");
        List<Child1> child2List = new ArrayList<>();
        child2List.add(child1);

        MongoUtil.persistToMongo(child2List, "someCollection");

    }
}

如果我尝试使用没有任何泛型的普通扩展类,它工作得很好,但随后我将不得不使用构建器模式进行权衡,在我的类中,我有太多的成员,而构建器模式可以轻松使用它们。 此外,我不想创建 persistToMongo 的单独方法采取List<Child1>List<Child2>分别。

任何帮助将不胜感激!

谢谢, 阿什特

最佳答案

我也遇到了这个问题,并删除了 spring-core 依赖。

关于java - 使用 Spring MongoTemplate 在 mongo 中插入数据时获取 java.lang.NoSuchMethodError : org. springframework.cglib.core.ReflectUtils.defineClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59575511/

相关文章:

java - Spring 2.0 和 2.3 版本的区别

node.js - 在 Express Web 应用程序中享受全面服务

MongoDB时间以字符串形式存储,如何过滤参数?

java - 使用 JPanel 初始化 JFrame 的正确方法

java - 如何使用 lambda 或流从包含另一个对象列表列表的对象列表中迭代并获取对象

Java - JComboBox 使 ItemListener 中的项目选择无效

java - HibernateDaoSupport 和原生 SQL

java - Hibernate OrphanRemoval 仅从关系表中删除数据

java - 使用 Spring 进行 MongoDB 复制

java - 将数据插入到通过 Java 动态更改的 MySql 表中