Backendless Support
 
Answered

Save data with object relation backendless 4.0 android

Hi i tried to save data to my backendless database from android sdk i already create two model files

the first one is Profil and the second is Sekolah and i have create the table on backendless database with one to many relation between profil and sekolah so one profile will have many sekolah.

this is my Sekolah and Profil .java class

  1. package id.putraprima.myapplication.models;

    import java.util.List;

    /**

    * Created on 15/11/17.

    */

    public class Profil {

    public String nama,nim;

    public List<Sekolah> riwayatSekolah;

    public Profil() {

    }

    public Profil(String nama, String nim, List<Sekolah> riwayatSekolah) {

    this.nama = nama;

    this.nim = nim;

    this.riwayatSekolah = riwayatSekolah;

    }

    public String getNama() {

    return nama;

    }

    public void setNama(String nama) {

    this.nama = nama;

    }

    public String getNim() {

    return nim;

    }

    public void setNim(String nim) {

    this.nim = nim;

    }

    public List<Sekolah> getRiwayatSekolah() {

    return riwayatSekolah;

    }

    public void setRiwayatSekolah(List<Sekolah> riwayatSekolah) {

    this.riwayatSekolah = riwayatSekolah;

    }

    }

  1. package id.putraprima.myapplication.models;

    /**

    * Created on 15/11/17.

    */

    public class Sekolah {

    public String namaSekolah,tingkatSekolah,tahunSekolah;

    public Sekolah() {

    }

    public Sekolah(String namaSekolah, String tingkatSekolah, String tahunSekolah) {

    this.namaSekolah = namaSekolah;

    this.tingkatSekolah = tingkatSekolah;

    this.tahunSekolah = tahunSekolah;

    }

    public String getNamaSekolah() {

    return namaSekolah;

    }

    public void setNamaSekolah(String namaSekolah) {

    this.namaSekolah = namaSekolah;

    }

    public String getTingkatSekolah() {

    return tingkatSekolah;

    }

    public void setTingkatSekolah(String tingkatSekolah) {

    this.tingkatSekolah = tingkatSekolah;

    }

    public String getTahunSekolah() {

    return tahunSekolah;

    }

    public void setTahunSekolah(String tahunSekolah) {

    this.tahunSekolah = tahunSekolah;

    }

    }

and the main activity is like this

  1. package id.putraprima.myapplication;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.util.Log;

    import android.widget.TextView;

    import android.widget.Toast;

    import com.backendless.Backendless;

    import com.backendless.async.callback.AsyncCallback;

    import com.backendless.exceptions.BackendlessFault;

    import java.util.ArrayList;

    import java.util.HashMap;

    import java.util.List;

    import java.util.Map;

    import id.putraprima.myapplication.config.Defaults;

    import id.putraprima.myapplication.models.Profil;

    import id.putraprima.myapplication.models.Sekolah;

    public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Backendless.setUrl( Defaults.SERVER_URL );

    Backendless.initApp( getApplicationContext(), Defaults.APPLICATION_ID, Defaults.API_KEY );

    Sekolah itemSekolah = new Sekolah("Nama","Tingkat","Tahun");

    List<Sekolah> listRiwayatSekolah = new ArrayList<>();

    listRiwayatSekolah.add(itemSekolah);

    // save object asynchronously

    Profil itemProfil = new Profil();

    itemProfil.setNama("Prima");

    itemProfil.setNim("0410630078");

    itemProfil.setRiwayatSekolah(listRiwayatSekolah);

    Backendless.Persistence.save(itemProfil, new AsyncCallback<Profil>() {

    @Override

    public void handleResponse(Profil response) {

    Toast.makeText(getApplicationContext(),"Saved",Toast.LENGTH_SHORT).show();

    Log.d("SOKO","Sukses");

    }

    @Override

    public void handleFault(BackendlessFault fault) {

    Toast.makeText(getApplicationContext(),"Unsaved "+fault.getMessage(),Toast.LENGTH_SHORT).show();

    Log.d("SOKO",fault.getMessage());

    }

    });

    //

    // Backendless.Persistence.save( itemSekolah, new AsyncCallback<Sekolah>() {

    // public void handleResponse( Sekolah response )

    // {

    // // new Contact instance has been saved

    // Toast.makeText(getApplicationContext(),"Saved",Toast.LENGTH_SHORT).show();

    // Log.d("SOKO","Sukses");

    // }

    // public void handleFault( BackendlessFault fault )

    // {

    // // an error has occurred, the error code can be retrieved with fault.getCode()

    // Toast.makeText(getApplicationContext(),"Unsaved "+fault.getMessage(),Toast.LENGTH_SHORT).show();

    // Log.d("SOKO",fault.getMessage());

    // }

    // });

    }

    }

the name and nim on profile class is saved succesfully to backendless database but the relation not saved. how do i fix this ? i already search on google and stackoverflow but it cant help.

Leave a Comment

Comments (7)

photo
1

Hi Putra,

Creating relations in 4.x works differently than in the previous version. Please see the documentation for details: https://backendless.com/docs/android/doc.html#related_objects

Regards,

Mark

photo
1

Hi Mark

Is there an example on asynchronous code i tried but stil i dont understand the process

photo
1

Hi.

I just want to clarify, what step do you not understand ?

1) Create two tables;

2) Create records in each table;

3) Create relations between two or more records;

For the last one see this article:

https://backendless.com/docs/android/doc.html#data_relations_api_set_add_android

Pay attention to the syntax of "relationColumnName" method parameter when set relation.

Examples: "myOrder:Order:1" or "myOrder:Order:n"

photo
1

Hi Oleg thank you for the explaination

i already create the relation on database and here is my code i am already create two table on backendless database and set relation between them but i am unable to save the data on the relation column here is my code :

https://pastebin.com/Q0u2uNpH

photo
1

You missed second item from the list.

2) Create records in each table;

 

Please, read the documentation (link above):

Other implementation details:

Both parent and child objects must exist in the Backendless storage.

photo
1

Hi oleg thanks for the help but i already check and both the data on both table are already saved i add the process to addRelation on handleResponse of my profilSave callback

photo
1

Hi oleg thanks for the help but i already check and both the data on both table are already saved i add the process to addRelation on handleResponse of my profilSave callback