Skip to content Skip to sidebar Skip to footer

Persisting Objects In Sugarorm

I have a Book class: public class Book extends SugarRecord { private String mBookName; private String mAuthorName; private List mPageList; public Book(

Solution 1:

No ORM database(SugarORm, DBFLow etc) supports List column. As you know sql don't have this datatype as well.

That's the reason why you are getting this error. If you ask me how you are saving list to ORM. I use Gson.

Declare Pagelist as string.

String Pagelist;

Before saving it to database convert it to Json string with the help Gson library.

Gsongson=newGson();
Stringvalue= gson.toJson(your_page_list);

when retrieving from database convert the json string to List using Gson.

List<Page> page_list;
  TypetypeIndicatorForGson=newTypeToken<ArrayList<Page>>() {}.getType(); 
 GsonmyGson=newGson();  
 page_list = myGson.fromJson(page_json_data_from_database, typeIndicatorForGson); 

Solution 2:

No List<Object> available on SugarORM. The way you can manage this is a little tricky. In few words, you can manage 1 to N relations, upside down. Take a look to the next example Lets suppose a Team object which can have N Person objects. Normally you will use a List<Person> in your class Team in this way:

publicclassTeam{
   String teamName;
   List<Person>
   ...
}

publicclassPerson{
   String name;
   String rol;
   ...
}

Well, it is not possible on SugarORM. But you can add Team as a property in Person, so, any Person's instance should contain a reference to the Team object it belong.

publicclassTeamextendsSugarRecord<Team> {
   String teamName;
   ...
}

publicclassPersonextendsSugarRecord<Person> {
   String name;
   String rol;
   Team team;
   ...
}

Then you can get all the Person objects from Team with a method (in the Team class) like:

publicclassTeamextendsSugarRecord<Team> {
   String teamName;
   ...

    public List<Person> getPersons(){
            return Person.find(Person.class, "id = ?", String.valueOf(this.getId()));
    }
}

So, you can manage 1 to N relations, but you can't manage N to M relationships (Person belonging to more than one Team object). IMO the way to manage this is using an Associative Entity in order to split N to M into two 1 to N relationships. As you can see SugarORM is not allowing you to think just in terms of objects, but, any case you can save a lot of boiler plate coding.

Post a Comment for "Persisting Objects In Sugarorm"