Reputation: 179
I'm writing a database for keeping information about words in Android application with Room. I've built two entities: Group and VocaNote. Between Group and VocaNote I use one-to-many relationship to keep array of VocaNote in separate groups.
Group class
@Entity(indices = {
@Index(value = {"language", "nameGroup"}, unique = true)
}
)
public class Group {
@PrimaryKey(autoGenerate = true)
public long id;
@ColumnInfo(name = "language")
public String language;
@ColumnInfo(name = "name_group")
public String nameGroup;
}
VocaNote class
@Entity(foreignKeys = @ForeignKey(entity = Group.class,
parentColumns = "id",
childColumns = "group_id",
onDelete = CASCADE))
public class VocaNote {
@PrimaryKey(autoGenerate = true)
public long id;
@ColumnInfo(name = "origin_word")
public String originWord;
@ColumnInfo(name = "translation")
public String translation;
@ColumnInfo(name = "studied_word")
public int studied;
@ColumnInfo(name = "group_id")
public int groupId;
}
After that I've added separate DAOs for above entity classes like that
VocaNoteDao
@Dao
public interface VocaNoteDao {
@Query("SELECT * FROM vocanote")
List<VocaNote> getAllVocaNote();
@Query("SELECT * FROM vocanote WHERE id = :id")
VocaNote getByIdVocaNote(long id);
@Insert
void insert(VocaNote vocaNote);
@Update
void update(VocaNote vocaNote);
@Delete
void delete (VocaNote vocaNote);
}
GroupDao
@Dao
public interface GroupDao {
@Insert
void insert(Group group);
@Update
void update(Group group);
@Delete
void delete (Group group);
@Query("SELECT * FROM group")
List<Group> getAllGroup();
@Query("SELECT * FROM group WHERE id = :id")
Group getByIdGroup(long id);
}
And here I got a problem. When I'm trying to make queries from group table, but instead recieve compile error table or subquery expected, got group. Please tell me, how I can deal with that? I have group entity but my IDE doesn't want to see it/
Upvotes: 5
Views: 3827
Reputation: 39
You must check for the Table name. It's mandatory to write table name.Not even a empty string.
Upvotes: -1
Reputation: 139
Use brackets to escape reserved keyword
@Query("SELECT * FROM [group] WHERE id = :id")
Group getByIdGroup(long id);
Upvotes: 8