I have just started working with Room and although everything seems to be pretty intuitive I currently don't really understand how exactly I could handle relationships.
Because SQLite is a relational database, you can specify relationships between objects. Even though most ORM libraries allow entity objects to reference each other, Room explicitly forbids this. Even though you cannot use direct relationships, Room still allows you to define Foreign Key constraints between entities.(Source: https://developer.android.com/topic/libraries/architecture/room.html#no-object-references)
How should you model a Many to Many or One to Many Relationship?
What would this look like in practice (example DAOs + Entities)?
You can use #Relation annotation to handle relations at Room.
A convenience annotation which can be used in a Pojo to automatically
fetch relation entities. When the Pojo is returned from a query, all
of its relations are also fetched by Room.
See document.
(Google's document has confusing examples. I have written the steps and some basic explanation at my another answer. You can check it out)
I created a simple Convenience Method that populates manually a one to many relationship.
So for example if you have a one to many between Country and City , you can use the method to manually populate the cityList property in Country.
/**
* #param tableOne The table that contains the PK. We are not using annotations right now so the pk should be exposed via a getter getId();
* #param tableTwo The table that contains the FK. We are not using annotations right now so the Fk should be exposed via a getter get{TableOneName}Id(); eg. getCountryId();
* #param <T1> Table One Type
* #param <T2> Table Two Type
* #throws NoSuchFieldException
* #throws IllegalAccessException
* #throws NoSuchMethodException
* #throws InvocationTargetException
*/
private static <T1, T2> void oneToMany(List<T1> tableOne, List<T2> tableTwo) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
String tableOneName = tableOne.get(0).getClass().getSimpleName();
String tableTwoName = tableTwo.get(0).getClass().getSimpleName();
for (T1 t1 :
tableOne) {
Method method = t1.getClass().getMethod("getId");
Integer pkId = (Integer) method.invoke(t1);
List<T2> listForCurrentId = new ArrayList<>();
for (T2 t2 : tableTwo) {
Method fkMethod = t2.getClass().getDeclaredMethod("get".concat(tableOneName).concat("Id"));
Integer fkId = (Integer) fkMethod.invoke(t2);
if (pkId == fkId) {
listForCurrentId.add(t2);
}
}
Method tableTwoList = t1.getClass().getMethod("set".concat(tableTwoName).concat("List"), List.class);
tableTwoList.invoke(t1, listForCurrentId);
}
}
This is how I use it .
SystemDefaults systemDefaults = new SystemDefaults();
return Single.zip(systemDao.getRoles(), systemDao.getCountries(), systemDao.getCities(), (roles, countries, cities) -> {
systemDefaults.setRoles(roles);
*ConvenienceMethods.oneToMany(countries,cities);*
systemDefaults.setCountries(countries);
return systemDefaults;
});
Related
I am fairly new to Android Room and SQLite in general, so sorry if this is a simple question.
I am getting data from a API that I'd like to insert into a database so it's accessible when the device is offline.
Depending on the endpoint of the API, some fields of my Data objects may be null (Think a summary with just the basic fields versus a fully detailed object with all fields)
To keep the database clean, I'd like to update the entries, but only the columns that are not null (eg. that I have new values for) and keep the rest of the columns untouched.
Here are some example classes to clarify:
Person
#Entity(tableName = "person", indices = {
#Index(value = "id", unique = true)
})
public class Person {
#PrimaryKey
public int id;
public String name;
public String description;
}
Example:
// create db
RoomDB db = RoomDB.create(ctx);
// create some sample objects
final Person p2 = new Person(2, "Peter", null);
// insert them into the db
db.personDao().insert(p2);
// create a updated peter that likes spiders
// but has no name (as a example)
final Person newPeter = new Person(2, null, "Peter likes spiders");
// and update him
db.personDao().updateNonNull(newPeter);
// now we read him back
final Person peter = db.personDao().getById(2);
In this example, the desired values of 'peter' would be:
id = 2
name = "Peter"
description = "Peter likes spiders"
However, using Room's #Update or #Insert i can only get this:
id = 2
name = null
description = "Peter likes spiders"
The only way i found to achive this would be to manuall get the object and supplement the values like so:
#Transaction
public void updateNonNull(Person newPerson) {
final Person oldPerson = getById(newPerson.id);
if (oldPerson == null) {
insert(newPerson);
return;
}
if (newPerson.name == null)
newPerson.name = oldPerson.name;
if (newPerson.description == null)
newPerson.description = oldPerson.description;
update(newPerson);
}
However, that would result in quite a bit of code with bigger objects...
So my question, is there a better way to do this?
Edit:
After some Testing with the SQL by #Priyansh Kedia, i found that those functions indeed work as intended and do so at a higher performance than java.
However, as a SQL statement would have required me to write huge queries, i decided to use a Reflection based solution, as can be seen below.
I only did so because the function isn't called regularly, so the lower performance won't matter too much.
/**
* merge two objects fields using reflection.
* replaces null value fields in newObj with the value of that field in oldObj
* <p>
* assuming the following values:
* oldObj: {name: null, desc: "bar"}
* newObj: {name: "foo", desc: null}
* <p>
* results in the "sum" of both objects: {name: "foo", desc: "bar"}
*
* #param type the type of the two objects to merge
* #param oldObj the old object
* #param newObj the new object. after the function, this is the merged object
* #param <T> the type
* #implNote This function uses reflection, and thus is quite slow.
* The fastest way of doing this would be to use SQLs' ifnull or coalesce (about 35% faster), but that would involve manually writing a expression for EVERY field.
* That is a lot of extra code which i'm not willing to write...
* Besides, as long as this function isn't called too often, it doesn't really matter anyway
*/
public static <T> void merge(#NonNull Class<T> type, #NonNull T oldObj, #NonNull T newObj) {
// loop through each field that is accessible in the target type
for (Field f : type.getFields()) {
// get field modifiers
final int mod = f.getModifiers();
// check this field is not status and not final
if (!Modifier.isStatic(mod)
&& !Modifier.isFinal(mod)) {
// try to merge
// get values of both the old and new object
// if the new object has a null value, set the value of the new object to that of the old object
// otherwise, keep the new value
try {
final Object oldVal = f.get(oldObj);
final Object newVal = f.get(newObj);
if (newVal == null)
f.set(newObj, oldVal);
} catch (IllegalAccessException e) {
Log.e("Tenshi", "IllegalAccess in merge: " + e.toString());
e.printStackTrace();
}
}
}
}
There is no in-built method in room to do this
What you can do is, put check in the query for your update method.
#Query("UPDATE person SET name = (CASE WHEN :name IS NOT NULL THEN :name ELSE name END), description = (CASE WHEN :description IS NOT NULL THEN :description ELSE description END) WHERE id = :id")
Person update(id: Int, name: String, description: String)
We have written the update query for SQL which checks if the inserted values are null or not, and if they are null, then the previous values are retained.
My Problem:
I'm struggling to eliminate the compiling error on the following Room #Query statement in a Room DAO. As you can see, the SQLite query statement is joining various fields from different tables. The missing fields identified by the error are a part of the Notes class constructor identified in the List type for the method. I think I need to change the List type identified. If I'm right, I need some guidance/suggestion on how I should resolve it. Do I need to create a new Class and DAO with just those specific fields queried? Or maybe just a class since there is not table specific to these fields only. The error is:
error: The columns returned by the query does not have the fields [commentID,questionID,quoteID,termID,topicID,deleted] in com.mistywillow.researchdb.database.entities.Notes even though they are annotated as non-null or primitive. Columns returned by the query: [NoteID,SourceID,SourceType,Title,Summary]
List getNotesOnTopic(String topic);
#Query("SELECT n.NoteID, s.SourceID, s.SourceType, s.Title, c.Summary FROM Comments as c " +
"LEFT JOIN Notes as n ON n.CommentID = c.CommentID " +
"LEFT JOIN Sources as s ON n.SourceID = s.SourceID " +
"LEFT JOIN Topics as t ON n.TopicID = t.TopicID WHERE t.Topic = :topic AND n.Deleted = 0")
List<Notes> getNotesOnTopic(String topic);
What I'm trying to do:
I'm attempting to convert and existing Java desktop app with an embedded an SQLite database. The above query does work fine in that app. I only want to pass field data from these tables.
What I've tried:
I've done some googling and visited some forums for the last few days (e.g. Android Forum, Developer.Android.com) but most of the Room #Query examples are single table full field queries (e.g. "Select * From table"). Nothing I found yet (there is probably something) quite addresses how and what to do if you are joining and querying only specific fields across tables.
I think I may have fixed my issue. I just created a new class called SourceTable and designated the queried fields in the constructor. The only catch was I, according to a follow up error, was that the parameters had to match the field names.
public class SourcesTable {
private int NoteID;
private int SourceID;
private String SourceType;
private String Title;
private String Summary;
public SourcesTable(int NoteID, int SourceID, String SourceType, String Title, String Summary){
this.NoteID = NoteID;
this.SourceID = SourceID;
this.SourceType = SourceType;
this.Title = Title;
this.Summary = Summary;
}
}
and then I update my list method:
List<SourcesTable> getNotesOnTopic(String topic);
Im using Realm v0.80.1 and I am trying to write migration code for a new property I added. The property is a RealmList. Im not sure how to properly add the new column or set a a value.
What I have:
customRealmTable.addColumn(, "list");
Once the column is properly added how would I go about setting an initial value for the list property? I would like to do something like:
customRealmTable.setRealmList(newColumnIndex, rowIndex, new RealmList<>());
As of Realm v1.0.0 (and maybe before), you can simply call RealmObjectSchema#addRealmListField(String, RealmObjectSchema) (link to javadoc) to achieve this. For example, if you're trying to add a permissions field of type RealmList<Permission> to your User class, you'd write:
if (!schema.get("User").hasField("permissions")) {
schema.get("User").addRealmListField("permissions", schema.get("Permission"));
}
There is also an example in Realm's migration docs here. And here is the full javadoc for addRealmListField, for convenience:
/**
* Adds a new field that references a {#link RealmList}.
*
* #param fieldName name of the field to add.
* #param objectSchema schema for the Realm type being referenced.
* #return the updated schema.
* #throws IllegalArgumentException if the field name is illegal or a field with that name already exists.
*/
You can see an example of adding a RealmList attribute in the examples here: https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/model/Migration.java#L78-L78
The relevant code is this section:
if (version == 1) {
Table personTable = realm.getTable(Person.class);
Table petTable = realm.getTable(Pet.class);
petTable.addColumn(ColumnType.STRING, "name");
petTable.addColumn(ColumnType.STRING, "type");
long petsIndex = personTable.addColumnLink(ColumnType.LINK_LIST, "pets", petTable);
long fullNameIndex = getIndexForProperty(personTable, "fullName");
for (int i = 0; i < personTable.size(); i++) {
if (personTable.getString(fullNameIndex, i).equals("JP McDonald")) {
personTable.getRow(i).getLinkList(petsIndex).add(petTable.add("Jimbo", "dog"));
}
}
version++;
}
GreenDao provides an addProtobufEntity method to let you persist protobuf objects directly. Unfortunately I can't find much documentation explaining how to use this feature.
Let's say I'm trying to add a foreign key into my Message entity so I can access its PBSender protobuf entity. Here's my generator code:
// Define the protobuf entity
Entity pbSender = schema.addProtobufEntity(PBSender.class.getSimpleName());
pbSender.addIdProperty().autoincrement();
// Set up a foreign key in the message entity to its pbSender
Property pbSenderFK = message.addLongProperty("pbSenderFK").getProperty();
message.addToOne(pbSender, pbSenderFK, "pbSender");
Unfortunately the generated code doesn't compile because it is trying to access a non-existant getId() method on my PBSender class:
public void setPbSender(PBSender pbSender) {
synchronized (this) {
this.pbSender = pbSender;
pbSenderID = pbSender == null ? null : pbSender.getId();
pbSender__resolvedKey = pbSenderID;
}
}
Can anybody explain how relationships to protocol buffer entities are supposed to be managed?
GreenDao currently only supports Long primary keys. Does my protobuf object need a method to return a unique Long ID for use as a primary key?
If I remove my autoincremented ID then the generation step fails with this error:
java.lang.RuntimeException: Currently only single FK columns are supported: ToOne 'pbSender' from Message to PBSender
The greenDAO generator Entity source code suggests it currently does not support relations to protocol buffer entities:
public ToMany addToMany(Property[] sourceProperties, Entity target, Property[] targetProperties) {
if (protobuf) {
throw new IllegalStateException("Protobuf entities do not support realtions, currently");
}
ToMany toMany = new ToMany(schema, this, sourceProperties, target, targetProperties);
toManyRelations.add(toMany);
target.incomingToManyRelations.add(toMany);
return toMany;
}
/**
* Adds a to-one relationship to the given target entity using the given given foreign key property (which belongs
* to this entity).
*/
public ToOne addToOne(Entity target, Property fkProperty) {
if (protobuf) {
throw new IllegalStateException("Protobuf entities do not support realtions, currently");
}
Property[] fkProperties = {fkProperty};
ToOne toOne = new ToOne(schema, this, target, fkProperties, true);
toOneRelations.add(toOne);
return toOne;
}
However, I suspect that you could make this work if your Protobuf class contains a unique long ID and a public Long getId() method to return that ID.
I'm tring to make join in two tables and get all columns in both, I did this:
QueryBuilder<A, Integer> aQb = aDao.queryBuilder();
QueryBuilder<B, Integer> bQb = bDao.queryBuilder();
aQb.join(bQb).prepare();
This equates to:
SELECT 'A'.* FROM A INNER JOIN B WHERE A.id = B.id;
But I want:
SELECT * FROM A INNER JOIN B WHERE A.id = B.id;
Other problem is when taking order by a field of B, like:
aQb.orderBy(B.COLUMN, true);
I get an error saying "no table column B".
When you are using the QueryBuilder, it is expecting to return B objects. They cannot contain all of the fields from A in B. It will not flesh out foreign sub-fields if that is what you mean. That feature has not crossed the lite barrier for ORMLite.
Ordering on join-table is also not supported. You can certainly add the bQb.orderBy(B.COLUMN, true) but I don't think that will do what you want.
You can certainly use raw-queries for this although it is not optimal.
Actually, I managed to do it without writing my whole query as raw query. This way, I didn't need to replace my query builder codes (which is pretty complicated). To achieve that, I followed the following steps:
(Assuming I have two tables, my_table and my_join_table and their daos, I want to order my query on my_table by the column order_column_1 of the my_join_table)
1- Joined two query builders & used QueryBuilder.selectRaw(String... columns) method to include the original table's + the columns I want to use in foreign sort. Example:
QueryBuilder<MyJoinTable, MyJoinPK> myJoinQueryBuilder = myJoinDao.queryBuilder();
QueryBuilder<MyTable, MyPK> myQueryBuilder = myDao.queryBuilder().join(myJoinQueryBuilder).selectRaw("`my_table`.*", "`my_join_table`.`order_column` as `order_column_1`");
2- Included my order by clauses like this:
myQueryBuilder.orderByRaw("`order_column_1` ASC");
3- After setting all the select columns & order by clauses, it's time to prepare the statement:
String statement = myQueryBuilder.prepare().getStatement();
4- Get the table info from the dao:
TableInfo tableInfo = ((BaseDaoImpl) myDao).getTableInfo();
5- Created my custom column-to-object mapper which just ignores the unknown column names. We avoid the mapping error of our custon columns (order_column_1 in this case) by doing this. Example:
RawRowMapper<MyTable> mapper = new UnknownColumnIgnoringGenericRowMapper<>(tableInfo);
6- Query the table for the results:
GenericRawResults<MyTable> results = activityDao.queryRaw(statement, mapper);
7- Finally, convert the generic raw results to list:
List<MyTable> myObjects = new ArrayList<>();
for (MyTable myObject : results) {
myObjects.add(myObject);
}
Here's the custom row mapper I created by modifying (just swallowed the exception) com.j256.ormlite.stmt.RawRowMapperImpl to avoid the unknown column mapping errors. You can copy&paste this into your project:
import com.j256.ormlite.dao.RawRowMapper;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.table.TableInfo;
import java.sql.SQLException;
public class UnknownColumnIgnoringGenericRowMapper<T, ID> implements RawRowMapper<T> {
private final TableInfo<T, ID> tableInfo;
public UnknownColumnIgnoringGenericRowMapper(TableInfo<T, ID> tableInfo) {
this.tableInfo = tableInfo;
}
public T mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
// create our object
T rowObj = tableInfo.createObject();
for (int i = 0; i < columnNames.length; i++) {
// sanity check, prolly will never happen but let's be careful out there
if (i >= resultColumns.length) {
continue;
}
try {
// run through and convert each field
FieldType fieldType = tableInfo.getFieldTypeByColumnName(columnNames[i]);
Object fieldObj = fieldType.convertStringToJavaField(resultColumns[i], i);
// assign it to the row object
fieldType.assignField(rowObj, fieldObj, false, null);
} catch (IllegalArgumentException e) {
// log this or do whatever you want
}
}
return rowObj;
}
}
It's pretty hacky & seems like overkill for this operation but I definitely needed it and this method worked well.