shadox shadox - 1 month ago 25
Android Question

Persisting array of strings with greenDao

I'm trying to map an object to database with greenDao. But when it comes to arrays, I don't know how to do it. After receiving JSON from network and deserializing it with GSON, I have objects defined by this class:

public class Car {
Long carId;
String name;
ArrayList<String> listOfLinks;
}


In case of a a different architecture, like this:

public class Car {
Long carId;
String name;
ArrayList<Link> listOfLinks;
}

public class Link {
Long carId;
String link;
}

----
Entity cars = schema.addEntity("Car");
cars.addLongProperty("carId").primaryKey();
cars.addStringProperty("name");

Entity links = schema.addEntity("Link");
links.addStringProperty("name");
links.addIdProperty().primaryKey().notNull().autoincrement();

Property linkProperty = links.addLongProperty("carId").getProperty();
ToMany carToLinks = cars.addToMany(link, linkProperty);


It would is easy. Define some relations, define properties, add foreign key and your done. With arrays I have no clue what to do. Ideas?

Answer

That approach is not common when using relational databases. This is commonly done using to-many relations : instead of using a list of String, you can create a Link entity and then use a list of Link.

Comments