'Getting java.lang.NoSuchMethodError: org.springframework.cglib.core.ReflectUtils.defineClass while inserting data in mongo using Spring MongoTemplate
I am trying to insert some data (List of Objects) into mongoDB using Spring MongoTemplate and I am getting an exception:
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)
Data is actually an Object which is created using builder pattern something like:
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() {};
}
Child class:
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;
};
}
Second Child class:
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 and Main class:
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");
}
}
If I try with normal extends class without any Generics, it works fine but then I will have to trade off with builder pattern and in my class I have way too many members and builder pattern makes using them easily.
Moreover, I don't want to create separate methods of persistToMongo
to take List<Child1>
and List<Child2>
separately.
Any help will be appreciated!.
Thanks, Ashit
Solution 1:[1]
I also encountered this problem and removed the spring-core dependency.
Solution 2:[2]
There was some dependency conflict problem because of which it was not happening. After resolving that, I am able to insert the documents now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | zzzyzz |
Solution 2 | ashit juneja |