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

Problem when use reflect in morphia #542

Closed
dingyaguang117 opened this issue Oct 20, 2013 · 1 comment
Closed

Problem when use reflect in morphia #542

dingyaguang117 opened this issue Oct 20, 2013 · 1 comment

Comments

@dingyaguang117
Copy link

public abstract class DomainBase {
    public void update(Datastore ds) {
        Class clazz = this.getClass();
        ObjectId id = null;
        UpdateOperations up = ds.createUpdateOperations(clazz).disableValidation();
        try
        {
            for(Field field : clazz.getDeclaredFields())
            {
                field.setAccessible(true);
                if (field.isAnnotationPresent(Transient.class)) continue;
                if (field.isAnnotationPresent(Id.class))
                {
                    id = (ObjectId)field.get(this);
                    continue;
                }
                if(field.get(this) != null)
                {
                    up = up.set(field.getName(),field.get(this));
                }
            }

            ds.update(ds.createQuery(clazz).filter("_id",id), up);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

when I use the way below:
ds.update(ds.createQuery(clazz).filter("_id",id),up)
will error when compile in ant :

Chinese:
对 update 的引用不明确 :
com.google.code.morphia.Datastore 中的 方法 update(T,com.google.code.morphia.query.UpdateOperations)
和 com.google.code.morphia.Datastore 中的 方法
update(com.google.code.morphia.query.Query,com.google.code.morphia.query.UpdateOperations)
都匹配

English:
update 's reference is ambiguous:
method in com.google.code.morphia.Datastore
update(T,com.google.code.morphia.query.UpdateOperations)
and method in com.google.code.morphia.Datastore
update(com.google.code.morphia.query.Query,com.google.code.morphia.query.UpdateOperations)
both matched

but it works in eclipse

so,I had to use the way below :
ds.update(ds.createQuery(clazz).filter("_id",id), up,false);
Is there any way to solve it?

@dingyaguang117
Copy link
Author

I solve it by indicate base class type "DomainBase"

public abstract class DomainBase {
    public void update(Datastore ds) {
        Class clazz = this.getClass();
        ObjectId id = null;
        
        
        UpdateOperations<DomainBase> up = ds.createUpdateOperations(clazz).disableValidation();
        try
        {
            for(Field field : clazz.getDeclaredFields())
            {
                field.setAccessible(true);
                if (field.isAnnotationPresent(Transient.class)) continue;
                if (field.isAnnotationPresent(Id.class))
                {
                    id = (ObjectId)field.get(this);
                    continue;
                }
                if(field.get(this) != null)
                {
                    up = up.set(field.getName(),field.get(this));
                }
            }
            Query<DomainBase> query = ds.createQuery(clazz).filter("_id",id);
            ds.update(query, up);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

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