Android Room: apply multiple columns in relation of embedded object
Asked Answered
T

0

5

In my Android App I am using Room for data storage. I am currently facing the problem, that I need to put 2 columns in my @Relation of the embedded object because the relation depends on 2 columns. See below the structure:

@Entity(tableName = "damages")
public class Damage {
    @PrimaryKey(autoGenerate = true)
    @NonNull
    private Long id;
    @NonNull
    private Long projectId;
    @NonNull
    private Long codeId;
    private String description;
    ...
}

@Entity(tableName = "damage_codes")
public class DamageCode {
    @PrimaryKey(autoGenerate = true)
    @NonNull
    private Long id;
    @NonNull
    private Long projectId;
    @NonNull
    private String name;
    private String description;
    @NonNull
    private String code;
    @NonNull
    private String damageType;
    ...
}

@Entity(tableName = "damage_types")
public class DamageType {
    @PrimaryKey(autoGenerate = true)
    @NonNull
    private Long id;
    @NonNull
    private Long projectId;
    @NonNull
    private String ttype;
    private String description;
    ...
}

public class DamageWithCode {
    @Embedded public Damage damage;
    @Relation(
        entity = DamageCode.class,
        parentColumn = "codeId",
        entityColumn = "id"
    )
    public DamageCodeAndType code;
    public Damage getDamage() {
        return damage;
    }
    public DamageCodeAndType getCode() {
        return code;
    }
}

public class DamageCodeAndType {
    @Embedded public DamageCode code;
    @Relation(
        parentColumn = "damageType",
        entityColumn = "ttype"
    )
    public DamageType damageType;
    public DamageCode getCode() {
        return code;
    }
    public DamageType getDamageType() {
        return damageType;
    }
}

In my DamageDAO I have the methods:

@Transaction
@Query("SELECT * FROM damages WHERE projectId=:projectId")
public List<DamageWithCode> getDamages(Long projectId);
@Query("SELECT * FROM damages WHERE id=:id")
public DamageWithCode getDamage(Long id);

The problem: In my DamageCodeAndType class in the @Relation I need to specify not only damageType as parentColumn but also the projectId. Additionally, in the entityColumn I need to add the projectId to the ttype column. How can I do that? Is something like this possible?

    public class DamageCodeAndType {
    @Embedded public DamageCode code;
    @Relation(
        parentColumn = {"damageType","projectId"},
        entityColumn = {"ttype","projectId"}
    )
    public DamageType damageType;
    public DamageCode getCode() {
        return code;
    }
    public DamageType getDamageType() {
        return damageType;
    }
}

If this is not possible, can anybody point me in the direction how to solve this. Thank you.

Tumer answered 18/1, 2021 at 8:54 Comment(4)
Did you find something?Hexapartite
From another post I learned that it is not possible (cannot find the post right now). Thus; I implemented an ugly work around which checks the data after it has been querried and read the correct data if requiered. In my case, as the number of digits should be in the sinle digits range, this is not a big issue but as I mentioned, it is an ugly solution.Tumer
has anybody found a better solutions for this?Withy
Yeah, this cannot be done as of now similar question is here. #51476131Forby

© 2022 - 2024 — McMap. All rights reserved.