【问题标题】:Getting java.lang.NoSuchMethodError: org.springframework.cglib.core.ReflectUtils.defineClass while inserting data in mongo using Spring MongoTemplate使用 Spring MongoTemplate 在 mongo 中插入数据时获取 java.lang.NoSuchMethodError: org.springframework.cglib.core.ReflectUtils.defineClass
【发布时间】:2020-01-03 08:49:07
【问题描述】:

我正在尝试使用 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;
      };
}     

第二个子类:

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 和 Main 类:

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&lt;Child1&gt;List&lt;Child2&gt;

任何帮助将不胜感激!

谢谢, 阿什特

【问题讨论】:

  • 嗨,有一些依赖冲突问题,因为它没有发生。解决这个问题后,我现在可以插入文档了。

标签: java spring mongodb spring-mongodb spring-mongo


【解决方案1】:

我也遇到了这个问题,把spring-core依赖去掉了。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

存在一些依赖冲突问题,因此没有发生。解决这个问题后,我现在可以插入文档了。

【讨论】:

  • 请指定哪个依赖冲突
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 2016-05-15
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
  • 2023-01-06
相关资源
最近更新 更多