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 :)
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.
This method is deprecated. Called by Room when it is initialized. Convenience method to query the database with arguments. Wrapper for SupportSQLiteDatabase. query .
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.
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.
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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With