Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disambiguate using @DocumentField in spring data mongoDB

While trying to save Object into mongo DB using Spring-Data MongoDB, I am getting this exception.

Ambiguous field mapping detected! Both <child class field> and <parent class field> map to the same field name <field name>! Disambiguate using @DocumentField annotation!

I am hiding a field in child class and this is causing the issue. But this is essential for me to hide the field declared in super class. I cannot find @DocumentField annotation anywhere. How can I proceed here? Is there any other solution?

like image 626
hnm Avatar asked Oct 23 '25 04:10

hnm


2 Answers

For those of you (like me) who arrived here due to another form of Ambiguous field mapping detected! error that is not quite the same as OP, consider that your child class may not even need a field at all and the answer provided by @sparm could be changed to:

public class Parent {
    private String myField;
    public getMyField() {
        return myField;
    }
}

@Document(collection = "children")
public class Child extends Parent {

    public getMyField() {
       super.getMyField();
    }

}
like image 118
Naruto Sempai Avatar answered Oct 24 '25 19:10

Naruto Sempai


I believe its an error in the exception. You need to use @Field instead and specify a different name for the parameter so:

public class Parent {
    private String myField;
}

@Document(collection = "children")
public class Child extends Parent {
    @Field("childField")
    private String myField;
}
like image 38
Sparm Avatar answered Oct 24 '25 19:10

Sparm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!