Room: Cannot use unbound fields in entities - android

I've found literally 0 articles/threads about this error on the internet so I'm absolutely clueless, so here it goes.
I have a class A and a class B.
class A is having an ArrayList of class B instances like this:
#Entity
class A {
#PrimaryKey
var id = 0
var listOfB: ArrayList<B>
}
And here it goes the B class:
#Entity
class B {
#PrimaryKey
var id = 0
var specificDate: Date? = null
}
Whenever I try to run the project I get the following error message:
Cannot use unbound fields in entities.
Which points exactly to the ArrayList of B instances inside the A class.
What may be causing this?

An Entity has two purposes:
It defines the columns of a table in the database.
An instance of the entity represents one row of an element in that database, which can be given to you as a query result, or you can pass to Room to add or edit a row in the corresponding table.
SQL/SQLite doesn't have the concept of a table column where the type is another table or list of rows from another table, so what you're defining doesn't make sense. (Incidentally, you cannot use Lists of any type for a column type in a table. You can only use primitive type classes in an Entity class since that's all that SQL supports.)
Instead, you must define a relationship between your two tables. In this case, you can remove the list property from your class A. I'm not sure if you're trying to define a one-to-many or many-to-many relationship, but you can look at the Room documentation here for an example of how to set up relationships.
Basically, if a B can only be a member of a single A at a time, B should reference an A row ID in one of its columns, and you have a one-to-many relationship. Room will let you create a third data class (not an entity) to define the relationship in a natural way that looks similar to your example class A and can be returned in queries.
If B can belong to multiple different A's, then you have a many-to-many relationship, which is more complicated. A third table (Entity class) has to be defined to link the relationships together.

Related

Android - Filter results by a relation column in parse server

I’m an Android developer working with back4app.com
Assuming there are two classes. Class1 and Class2.
Class1 has a relation column to Class2.
I need to query Class1 and find the rows which their relation column, contains a specific Class2 object. The specific Class2 object is available.
I think that one way is to fetch all rows of Class1, and check if the relation column contains that specific Class2 object for each row. But I believe it is the worst-practice possible :)
So is there any other way to achieve this in android?
Something like this should work:
ParseQuery query = ParseQuery.getQuery("Class1");
query.whereEqualTo("theNameOfTheRelationColumn", theClass2Object);
You can see more details about Many-To-Many relations here.

Android : Can I use My Entity class using DISTINCT query Room Database

I am using Room Database for storing chat, I have one group table and I want to fetch three parameters like this query
My Group Entity class is GroupUserEntity.kt
#Query("SELECT DISTINCT group_id, group_name, group_image FROM group_user_table")
fun getAllGroups() : List<ENTITY_CLASS>
I am not able to use my Group Entity class(GroupUserEntity.kt) over ENTITY_CLASS, forcefully I have to make new Entity class(Pojo Class) to fetch those three values.
So my questions is, there is any way to utilize my old Entity class(GroupUserEntity.kt) to fetch all groups? or I need to make a new POJO class for that?
It depends on number of variables in your GroupUserEntity.kt.
If for example you have group_id, group_name, group_image and group_category (total 4) in GroupUserEntity.kt and you are fetching data for only 3 variables then it will show you an error. (Cursor mismatch).
To avoid this either call ALL the columns values from database to match GroupUserEntity.kt, or create a new POJO.

android- Multiple tables of the same class in Room Database with no relation to each other

I'm using Room as the database for the app. I have a scenario where an Object of a certain type needs to be stored in separate tables. As an example, lets take the Object called Item.java
Now, I want to have three SQL tables:
Item1
Item2
Item3
ignore any naming conventions for SQL DB please - this is just an example
Problem:
I have an entity as #Entity(tableName="Item1") in the Item.java class and have a DAO class that will use the table name.
I am having 3 fragments, each being independent of each other. I just want three database with same attributes and functions as Item. I want to create another table Item2 of same Item.java class to use it in the second fragment. And one more table Item3 of same Item.java class for third fragment.
One solution is to create another class Item2.java and Item3.java and extend Item.java to it and create the DAO and Room Database for that class. But it would be redundant.
Question:-
Is there any other way i can create multiple tables of same class having no relation to each other?
Why don't you add another column in the Item table indicating the fragment, and index the table on that column? This way, all your data will be stored in the same table, at the same time differentiated by the new column, and the index will ensure that you get faster retrievals for any of the fragment.

Room database many to many relation get from couple tables one response

I have three tables Notes, Tags and join table with foreign keys of Note and Tag called NoteTagJoin, but how can I return Note with all Tags as one response?
Here is query to get Note and Tags:
SELECT n.*,t.* FROM notes n INNER JOIN note_tag_join nt ON n.entryId = nt.noteId INNER JOIN tags t ON t.tagId = nt.tagId WHERE n.entryId =:noteId
And here is response class which has to hold note and list of tags:
data class NoteResponse(
#Embedded
var note: Note? = null,
#Relation(parentColumn = "entryId", entityColumn = "tagId", entity = Tag::class)
var tags: List<Tag>? = null
)
But tags list is empty on response just note is there, I know for sure that Tags and Note exists in db and Join table has right foreign keys because all other queries works so my guess is that Query is wrong or NoteResponse class is wrong because I use annotation #Relation which I don't need, but if I don't add #Relation annotation on tags it throws error that room doesn't know what is this list so how to do it? I can't find any references for this, documentation only mentions embedding one class in POJO but no examples for Lists and all similar posts talks only about inserting list.
please see my realization of many-to-many relationship CinemaActorJoinDao
You can instead my code , replace with your , if you have any question , i will try ask to you :)

Room; Store integer array to a separate table?

Using Room ORM, I have declared an entity EQPreset using #Entity annotation. The entity contains an array int[]. It gives following error:
error: Cannot figure out how to save this field (int[] arr) into database. You can consider adding a type converter for it.
Normally saving EQPreset instance to a database, I would create a separate table to store values of the array and have a foreign key pointing to the relevant EQPreset.
However, I need to find what will be the way of storing this int[] arr of EQPreset using Room, that is, either by making a separate table or using any good approach/way.
Option #1: Get rid of int[] arr. Have some other entity represent this integer, with a foreign key back to the EQPreset entity. Have methods on your DAO be able to give you the integer-entities for a given EQPreset entity.
Option #2: Use a #TypeConverter to convert the int[] into something that can go into a single column (e.g., convert it to and from a JSON array, represented as a string).

Categories

Resources