Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support persistence of dynamically generated CGLib Beans [DATAMONGO-762] #1692

Closed
spring-projects-issues opened this issue Sep 25, 2013 · 4 comments
Assignees
Labels
in: core type: enhancement

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Sep 25, 2013

Sathish Kumar Thiyagarajan opened DATAMONGO-762 and commented

Steps:

  1. Generate a Dynamic Bean Class using CGLib
    Name of the Bean: ProxyBean
    Name of the Property: properetyName

  2. Create Instance of the ProxyBean.

  3. Populate value for the "propertyName"

  4. 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:

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 30, 2013

Sathish Kumar Thiyagarajan commented

Patch that solves this issue

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 30, 2013

Sathish Kumar Thiyagarajan commented

Found a partial Fix for the problem.

  1. 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.

    ((MongoMappingContext) mongoOperation.getConverter().getMappingContext()).setFieldNamingStrategy(new FieldNamingStrategy() {
        @Override
        public String getFieldName(MongoPersistentProperty property) {
            return property.getName().replaceAll("\\$cglib_prop_", "");
        }
    });
    
  2. 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

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Mar 5, 2014

Thomas Darimont commented

Hello Sathish,

thanks for the workaround - we're considering this as a new feature for the upcoming development iterations.

Cheers,
Thomas

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Mar 2, 2020

Christoph Strobl commented

Proxy resolution has been solved in the past few years. This works as designed on current master (3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants