javaandroiddatabasebackendless

How to save object with relation using JAVA in Backendless 4.0?


(SAVING DATA WITH RELATION USING BACKENDLESS 4.0)

I would like to save data with one to one relation. example: -person_table have a relation with adress_table the data is save in person_table and address_table without relation. how can make this? #backendless4.0

Adress newAdress= new Adress();
    newAdress.setAdress("43 Street");
    newAdress.setPersonId(Person);
    Backendless.Persistence.save(newAdress, new AsyncCallback< Adress >() {
        @Override
        public void handleResponse(Adress response) {
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            fault.getMessage();
        }
    });

Solution

  • I find the result: for save with relation in backendless 4.0 you need to save the object first and save the relation after.

    ref link 1 : ADD RELATION WITH OBJECTS ref link 2 : save object with relation using REST API

    Person personObject = // personObject retrieval is out of scope in this example
            Adress objectAdress = // addressObject retrieval is out of scope in this example
            ArrayList< Person > personCollection = new ArrayList< Person >();
            personCollection.add( personObject );
            Backendless.Data.of( Adress.class ).addRelation( objectAdress, "personId:Adress:n", personCollection,
                new AsyncCallback<Integer>()
                {
                    @Override
                    public void handleResponse( Integer response )
                    {
                        progressDialog.dismiss();
                        Log.i( "MYAPP", "related objects have been added");
                    }
    
                    @Override
                    public void handleFault( BackendlessFault fault )
                    {
    
                    }
                } );