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 for UpdateOperations#set(String, Object) with @Reference on the attribute. [moved] #191

Closed
jyemin opened this issue Apr 25, 2013 · 0 comments

Comments

@jyemin
Copy link
Contributor

jyemin commented Apr 25, 2013

This is Issue 191 moved from a Google Code project.
Added by 2010-12-22T02:04:03.000Z by mrbur...@gmail.com.
Please review that bug for more context and additional comments, but update this bug.
Closed (Fixed).

Original labels: Type-Defect, Priority-Low

Original description

Problem code
        Query<UserGroup> query = createQuery()
                .field(&quot;_id&quot;).equal(group.getId());
        query.and(query.criteria(&quot;user&quot;).doesNotExist());

        UpdateOperations<UserGroup> ops = getDatastore()
                .createUpdateOperations(UserGroup.class)
                .set(&quot;user&quot;, user);
        update(query, ops);

Solution (Per skot)
        Query<UserGroup> query = createQuery()
                .field(&quot;_id&quot;).equal(group.getId());
        query.and(query.criteria(&quot;user&quot;).doesNotExist());

        UpdateOperations<UserGroup> ops = getDatastore()
                .createUpdateOperations(UserGroup.class)
                .set(&quot;user&quot;, getDatastore().getKey(user));
        update(query, ops);

The user in UserGroup is decorated with @Reference. Instead of the object being set as a DBRef, it used a BasicDBObject which resulted in the exception below.

<b>What version are you using? (Morphia/Driver/MongoDB)</b>
0.99-SNAPSHOT

<b>Please include a stack trace below:</b>
java.lang.RuntimeException: java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.mongodb.DBRef
    at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:421)
    at com.google.code.morphia.mapping.Mapper.fromDBObject(Mapper.java:265)
    at com.google.code.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:53)
    at com.google.code.morphia.query.MorphiaIterator.next(MorphiaIterator.java:48)
    at com.google.code.morphia.query.QueryImpl.get(QueryImpl.java:340)
    at com.google.code.morphia.DatastoreImpl.get(DatastoreImpl.java:498)
    at com.google.code.morphia.dao.BasicDAO.get(BasicDAO.java:163)
@jyemin jyemin closed this as completed Apr 25, 2013
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

1 participant