0

Why firebase writes the method name "_UserName" instead of its value "_n" ?

Model class:

public class UserModel {    
    private String _n;  
    ....  
    public UserModel()  
    { }  
  
    public UserModel(  
  
     
          String _n,  
            ....  
    )  
    {  
        this._n = _n;  
       ....  
    }  
  
  
    public String get_UserName() {  
        return _n;  
    }  
  
    public void set_UserName(String _n) {  
        this._n = _n;  
    } 
} 

Create data:

 DatabaseReference UserRef = FirebaseDatabase.getInstance().getReference()  
                .child("allUsers");  
final UserModel newUser = new UserModel();    
  
newUser.set_UserName("test new User 1");  
....  
  
final HashMap<String, Object> userMap = new HashMap<>();  
productMap.put($newUserID, productNew);  
  
UserRef  
        .updateChildren(userMap)  
        .addOnCompleteListener(new OnCompleteListener<Void>() {  
            @Override  
            public void onComplete(@NonNull Task<Void> task)  
            {  
                if (task.isSuccessful())  
                {  
                      
                    Toast.makeText(getBaseContext(), "User is added successfully..", Toast.LENGTH_SHORT).show();  
  
                }  
                else  
                {  
                    loadingBar.dismiss();  
                    String message = Objects.requireNonNull(task.getException()).toString();  
                    Toast.makeText(getBaseContext(), "Error: " + message, Toast.LENGTH_SHORT).show();  
                }  
            }  
        }); 

Writes in Firebase:

{ allUsers: [ "$newUserID" :{ "_UserName": "test new User 1" .... }, ... ] }

When rename "set_UserName" to "set_UserNameBlaBla3563636" then such key turns out:

{ allUsers: [ "$newUserID" :{ "_UserNameBlaBla3563636": "test new User 1" .... }, ... ] }

When I save this information on my android, everything is fine:

{ allUsers: [ "$newUserID" :{ "_n": "test new User 1" .... }, ... ] }

Why firebase writes "_UserName" instead of "_n" ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Can you please rephrase "Why firebase writes the method name "_UserName" instead of its value "_n" ?"? – Alex Mamo Aug 22 '21 at 13:24
  • I need the data to be written in this form in my database: ".... "_n": "test Name User 1", .... ". At the moment, for some reason, written in the database: ".... "_UserName": "test Name User 1", .... " I do not understand why this is happening? Thanks for the help and response. –  Java NewUser Aug 22 '21 at 13:58
  • Ok, I'll write you an answer right away. – Alex Mamo Aug 22 '21 at 14:02
  • 1
    @FrankvanPuffelen That was fast :) – Alex Mamo Aug 22 '21 at 14:05

1 Answers1

1

Firebase uses JavaBean naming conventions to map the members of your Java class to properties in the JSON database. So instead of using the name of the field (_n) it uses the getter (public String get_UserName()) and setter (public void set_UserName(String _n)) to determine that there is a property _UserName.

If you want to control the names that Firebase uses in the mapping, you can add a PropertyName annotation to the getter and setter:

@PropertyName("_n")
public String get_UserName() {  
    return _n;  
}  

@PropertyName("_n")
public void set_UserName(String _n) {  
    this._n = _n;  
} 

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807