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

ClassCastException when calling PathMetadata.getName() #512

Closed
mnshdw opened this issue Sep 23, 2013 · 2 comments
Closed

ClassCastException when calling PathMetadata.getName() #512

mnshdw opened this issue Sep 23, 2013 · 2 comments

Comments

@mnshdw
Copy link

mnshdw commented Sep 23, 2013

Hello,

I recently upgraded from querydsl 2.9.0 to 3.2.3 and spring-data-mongodb 1.2.1.RELEASE to 1.3.1.RELEASE.

Currently, some of my MongoDB entities have inner objects stored using maps with Long keys :

@Document(collection = "xxx")
@QueryEntity
public class ObjectA {
    private Map<Long, ObjectB> objects;
}

When querying whether a key is present in the map (for example QObjectA.objectA.objects.get(objectId).isNotNull()), I now get a ClassCastException :

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
    at com.mysema.query.types.PathMetadata.getName(PathMetadata.java:70)
    at org.springframework.data.mongodb.repository.support.SpringDataMongodbSerializer.getKeyForPath(SpringDataMongodbSerializer.java:66)

The getKeyForPath() method has changed between the two spring-data-mongodb releases : it was metadata.getExpression().toString() in the previous version, not getName(). However I'm not really sure whether the culprit is spring-data-mongodb or query-dsl. I noticed the PathMetadata code was rather "odd" : the generic type is never used, and the getName() method blindly casts the element to a String.

public String getName() {
    return (String)element;
}

Is this issue only related to query-dsl ?
Should I ask the spring-data guys for help ?
Any pointers in helping resolve this issue would be much appreciated.

@timowest
Copy link
Member

This should be safer than the previous behaviour of taking the generic key object and calling toString(). You will get a ClassCastException for cases where the last path stage doesn't refer to a property.

This looks like a Spring Data issue.

@timowest
Copy link
Member

I added a better Exception to guide the user to use getElement() instead for certain PathMetadata usage.

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

No branches or pull requests

2 participants