Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room multiple tables same Object (multi language database)

I am working on an android project where have to store some data in the local DB (Room). One of the functionality which I have to provide is to store the data in the local DB in different languages, for example if I have information for food, this information has to be stored in English, German, French and so on.

The structure of my DB is something like that:

@Entity(tableName = "food")
public class Food{

}

@Entity(tableName = "food_fr")
public class FoodFr{

}

@Entity(tableName = "food_de")
public class FoodDe{

}

My question is how I can have these three different tables (on different languages) with same columns and the @Dao object return one common (parent) object for all of them?

I am not really sure that is possible at all, but if someone has a solution for that case, please help.

Thanks in advance :)

like image 981
MrVasilev Avatar asked May 08 '18 12:05

MrVasilev


People also ask

Can 1 database have multiple tables?

The majority of databases you'll work with as a developer will have more than one table, and those tables will be connected together in various ways to form table relationships.

Is room database deprecated?

This method is deprecated. Called by Room when it is initialized. Convenience method to query the database with arguments. Wrapper for SupportSQLiteDatabase. query .

Why was it advantageous to store data across multiple tables rather than storing all of the data in a single table?

In many cases, it may be best to split information into multiple related tables, so that there is less redundant data and fewer places to update.

Can we fetch data from multiple tables using one query?

To retrieve information from more than one table, you need to join those tables together. This can be done using JOIN methods, or you can use a second SELECT statement inside your main SELECT query—a subquery.


2 Answers

The best solution is to have a single table, rather than three tables. Use a column to distinguish between the three languages (e.g., a language column with en, fr, and de values). Since you will be rewriting much of your existing code anyway, switching from three tables to one would not seem to be a major impediment.

That being said, to keep your existing three-table structure, have Food, FoodFr, and FoodDe all extend from a common base class (e.g., BaseFood), where you define your fields/columns.

For queries, you would need to have your DAO handle all four cases (three specific language tables, plus a method to combine the results for all three), such as:

@Query("SELECT * FROM Food")
List<Food> getAllFood();

@Query("SELECT * FROM FoodFr")
List<FoodFr> getAllFrenchFood();

@Query("SELECT * FROM FoodDe")
List<FoodDe> getAllGermanFood();

@Transaction
List<BaseFood> getAllFoodAcrossAllThreeLanguages() {
  ArrayList<BaseFood> result=new ArrayList<>();

  result.addAll(getAllFood());
  result.addAll(getAllFrenchFood());
  result.addAll(getAllGermanFood());

  return result;
}
like image 110
CommonsWare Avatar answered Sep 29 '22 18:09

CommonsWare


You can merge different tables with @Embedded annotation. https://developer.android.com/reference/android/arch/persistence/room/Embedded?authuser=4

And in Dao class write a sql query that merge your tables with inner join and return that merged object.

You can look at my example application. https://github.com/volkansahin45/Moneycim

In Model/Pojo folder there is a Spending class and in Model/Dao folder there is SpendingDao class

like image 36
VolkanSahin45 Avatar answered Sep 29 '22 19:09

VolkanSahin45