abc
abc

Reputation: 2027

Using Room with Relations returning an error

I have 3 entities, A, B, C.

Entity A relates to B and C.

In order to model this relation I did this:

public class RelationA {
   @Embedded public A a;

   @Relation(parentColumn = "ID", entityColumn = "bID", entity=B.class)
   public B b;

   @Relation(parentColumn = "ID", entityColumn = "cID", entity=C.class)
   public C c;
}

I have a Dao for this Relation like this:

@Dao
public abstract class RelationADao extends BaseDao<RelationA> {
    // Add more methods for more custom operations
}

With the usual BaseDao with insert,update,delete operations for the generic type T.

Here is my RoomDatabase:

@Database(entities = {A.class, B.class, C.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
   public abstract RelationADao relationADao();

   // initialization code here...
}

I'm getting the follwing error and I cannot understand why.

 error: Type of the parameter must be a class annotated with @Entity or a collection/array 
 of it.
     public abstract void insert( T entity);`
                               ^

If you are curious about the BaseDao implementation I will include it:

@Dao
public abstract class BaseDao<T> {
    @Update
    public abstract void update( T entity);

    @Delete
    public abstract void delete( T entity);

    @Insert
    public abstract void insert( T entity);
}

My understanding is that I can set up a Relation and a corresponding Dao and update the individual entities through the relation. Could be this is where I went wrong if that's the case how would I achieve inserting into other entities using the relation?

Any help would be greatly appreciated thank you!

Upvotes: 0

Views: 71

Answers (1)

sergiy tykhonov
sergiy tykhonov

Reputation: 5103

My understanding is that I can set up a Relation and a corresponding Dao and update the individual entities through the relation. Could be this is where I went wrong if that's the case how would I achieve inserting into other entities using the relation?

No, your RelationA class has only one useful function - it can be used to get result with this type from query. You cannot use it to insert/update/delete values, as in classes annotated with @Entity. Equivalent of your two Relations in your class in ROOM - is set of two Left Joins (B,C tables) in SQL query added to table A.

There is no corresponding table in SQLite connected to it. So all changes you have to do with your A, B, C classes with the help of their Dao's.

Upvotes: 1

Related Questions