Generate a Dynamic Bean Class using CGLib
Name of the Bean: ProxyBean
Name of the Property: properetyName
Create Instance of the ProxyBean.
Populate value for the "propertyName"
Save the record with MongoDB
Expected Result:
A new Record should get added with Collection Name as 'ProxyBean' and field name as 'propertyName'
Actual Result:
Getting an Exception as given below
Exception in thread "main" java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$cglib_prop_propertyName')
at com.mongodb.DBCollection.validateKey(DBCollection.java:1450)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:1410)
at com.mongodb.DBCollection._checkObject(DBCollection.java:1397)
at com.mongodb.DBCollection.save(DBCollection.java:800)
at com.mongodb.DBCollection.save(DBCollection.java:786)
Latest release from Spring release (Eg. 1.3.1.RELEASE) expose the method to set FieldNamingStrategy from MongoMappingContext. Below code will solve the issue for all fields except ID Property.
For ID Property, Spring Mongo uses separete flow to find the approperiate Name. ( Through persistant Entity). So ID names has to be proper always. We can solve the issue my making CGLib Dynamic Beans extend a Java Class that has defined a proper ID Field with Annotation.
E.g:
public class BaseObject {
@Id
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
PFA Spring-Mongo-CGLib-Patch.zip for the working patch
Sathish Kumar Thiyagarajan opened DATAMONGO-762 and commented
Steps:
Generate a Dynamic Bean Class using CGLib
Name of the Bean: ProxyBean
Name of the Property: properetyName
Create Instance of the ProxyBean.
Populate value for the "propertyName"
Save the record with MongoDB
Expected Result:
A new Record should get added with Collection Name as 'ProxyBean' and field name as 'propertyName'
Actual Result:
Getting an Exception as given below
Exception in thread "main" java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$cglib_prop_propertyName')
at com.mongodb.DBCollection.validateKey(DBCollection.java:1450)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:1410)
at com.mongodb.DBCollection._checkObject(DBCollection.java:1397)
at com.mongodb.DBCollection.save(DBCollection.java:800)
at com.mongodb.DBCollection.save(DBCollection.java:786)
Reference:
PAF Maven Project with main Class App.java
Affects: 1.2.3
Attachments:
The text was updated successfully, but these errors were encountered: