I can't figure out how to bind a subquery select within an attribute of the entity, I simply need to bind the "main_game" subquery into the mainGame String attribute of my entity:
My DAO:
@Dao
public interface GamesDao {
@Query("SELECT *, (SELECT g_nested._id FROM games as g_nested, game_groups as gg WHERE g_nested._id = gg._game_group AND g_nested.is_main = 1 AND gg._game = g._id) as main_game FROM games as g")
List<GameEntity> getAll();
}
My GameEntity:
@Entity(tableName = "games")
public class GameEntity extends GenericIndexEntity {
@NonNull
@ColumnInfo(name = "gen")
private Integer gen;
@NonNull
@ColumnInfo(name = "is_main")
private Integer isMain;
@Ignore
@ColumnInfo(name = "main_game")
private String mainGame;
public GameEntity(@NonNull String indexNumber, String name, String nameEs, String nameIt, String nameFr, String nameDe, String namePt, String nameRu, @NonNull Integer gen, @NonNull Integer isMain, String mainGame) {
super(indexNumber, name, nameEs, nameIt, nameFr, nameDe, namePt, nameRu);
this.gen = gen;
this.isMain = isMain;
this.mainGame = mainGame;
}
@NonNull
public Integer getGen() {
return gen;
}
public void setGen(@NonNull Integer gen) {
this.gen = gen;
}
@NonNull
public Integer getIsMain() {
return isMain;
}
public void setIsMain(@NonNull Integer isMain) {
this.isMain = isMain;
}
public String getMainGame() {
return mainGame;
}
public void setMainGame(String mainGame) {
this.mainGame = mainGame;
}
}
I've tried using multiple combinations of @Ignore, @ColumnInfom removing them but it simply doesn't bind the value:
@Ignore
@ColumnInfo(name = "main_game")
private String mainGame;
This way is throwing Tried the following constructors but they failed to match: ... param:mainGame -> matched field:unmatched]
If I remove all the tags from the attribute the query works, but doesn't populate the value.
Am I missing something?
If you
@Ignorea member variable then Room discounts considering that member variable as a column. If you do not@Ignorethe member variable it WILL be a column in the table.Assuming that you do not want the member variable to be a column in the table BUT at some time you want to retrieve and assign a value then you will need to use a POJO that either has a member variable that is not
@Ignoredperhaps in your case an extra member variable (due to the complexity of extended classes).Due to the apparent complexity of the table i.e. GameEntity extends GenericIndexEntity and possibly other classes are extended, it would be possibly be a nightmare to hard code all the involved columns.
Thus you may well want to use the
@Embeddedannotation in the POJO to make life easy. However, doing so will include the@Ignored main_game column. So you would need to use another member variable.So something like:-
In which case you would want to output a column named mg and then perhaps use the getGameEntity method which should return a GameEntity with the main_game set with the extracted value.
Demo
Here's a demo based upon the available code, with other code made up and importantly a query that a) gets the GameEntity columns (including the columns from the extended GenericIndexEntity *(my cobbled together version)).
To demo this a simplified query was used, The
@Daoannotated interface used being:-The full, often cobbled together, code used for the demo (less the GamesDao interface as above):-
GenericIndexEntity (cobbled together)
GameEntity (adjusted to overcome some unknowns/guesses):-
GameWithMainGamePOJO (as above)
TheDatabase the
@Databaseannotated abstract class:-MainActivity to demonstrate the principle:-
The RESULT output to the log:-
As can be seen MG11/MG12 has been dynamically generated and applied and also that the getGameEntity method returned the expected HameEntity with mainGame set correctly.