GreenDao, how delete relationship entity - android

I have EntityA and EntityB. EntityA has a to-many relationship with EntityB.
I have several EntityB objects that have been set as related (children) to EntityA. How can I delete that relationship without deleting any of the Entity objects?

Try the following:
EntityB entityB = myEntityBDao.load(id_entityB);
entityB.setEntityA(null);
entityB.update();
This just deletes the relation and modifies entityB, but a formerly related EntityA should not be deleted or even modified.

Related

Room: Cannot use unbound fields in entities

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.

Android - Migrating to Room Database

I need to migrate from Sqlite database to room in my application, but before migrating i have some doubts regrading room database i have searched through many sites but i have not satisfied with the solution.
My table schema will be changing frequently through API calls, is it possible in room ?
We need entities to map Column Name with entity properties, Since my table schema changes frequently how do i create the entity ?
Is it possible to map the multiple columns to single field property in entity, like say
Table A has three columns COL_A,COL_B,COL_C
#Entity
class TableA{
#ColumnInfo("COL_A")
val columnA : String
#ColumnInfo("COL_B,COL_C")
val columns : Map<String,String>
}
Is it possible to create entity like above in room?
Can anyone clear the above doubts ?

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.

How to set list data by greenDAO to many relation?

this is a example:
User and Orders. 1:N relation
but UserDAO do not have a method called "setOrders(List...)"
only getOrdersList() and resetXXXList()
You have to do it from the other part of the relation, Orders, using something like order.setUser(user) and updating the entity in the DB.

How to save one-to-one mapping entity in greendao?

I have entityA and entityB that have a one-one relationship between them (entityA.a = entityB).
My question is: if I set entityA.a = entityB, then insert entityA, will entityB automatically be inserted?
I think it should be, but it looks like it's not.
Does that mean I should insert entityB myself?
By the way, I know I should call insert myself for a one-to-many relationship.
No, it won't be inserted. A relation is a link between two existing entities, you should first insert both entities and set de relation after.

Categories

Resources