Hello,
I’m trying to make a select in Room to get the list of Clients with the Civilite, Telephone and Email objects inside
I tried various solutions also by creating a join in sql, but I can’t get the other objects
@Query(“SELECT Client., Civilite. FROM Client LEFT OUTER JOIN Civilite ON Client.idCiviliteJoin = Civilite.idCivilite”)
fun getClients(): List
These are my classes / tables
@Entity(tableName = "Client", indices = {@Index(value="idClient",unique = true),})
public class Client {
@PrimaryKey
@SerializedName("id")
@NonNull
private String idClient;
@Nullable
private Civilite civilite;
@Nullable
private Long idCiviliteJoin;
@Ignore
private List<Telephone> telephones;
@Nullable
private Long idEmail;
@Nullable
private Long idEmailProf;
}
@Entity(tableName = "Telephone", indices = {@Index(value = {"typeTelephone", "numero"}, unique = true)})
public class Telephone {
@PrimaryKey(autoGenerate = true)
private long idTelephonePK;
private String typeTelephone;
private String numero;
}
@Entity(tableName = "Email", indices = {@Index(value="idEmail ",unique = true),})
public class Email{
@PrimaryKey
@SerializedName("id")
@NotNull
private Long idEmail ;
private String email;
}
@Entity(tableName = "Civilite", indices = {@Index(value="idCivilite",unique = true),})
public class Civilite {
@PrimaryKey
@SerializedName("id")
@NotNull
private Long idCivilite ;
private String intitule;
}
I have created a join class
@Entity(tableName = "ClientCiviliteJoin",
primaryKeys = { "idClient", "idCivilite" },
foreignKeys = {
@ForeignKey(entity = Client.class,
parentColumns = "idCiviliteJoin",
childColumns = "idClient"),
@ForeignKey(entity = Civilite.class,
parentColumns = "idCivilite",
childColumns = "idCivilite")
})
public class ClientCiviliteJoin {
public final int idClient;
public final int idCivilite;
public ClientCiviliteJoin(final int idClient, final int idCivilite) {
this.idClient = idClient;
this.idCivilite = idCivilite;
}
by running a query like this
@Query("SELECT * FROM Client INNER JOIN clientcivilitejoin ON client.idClient=clientcivilitejoin.idClient") fun getClientsJoin(): List<Client>
but I get this error, maybe because the idCiviliteJoin field is not a key, but I still don’t understand how to create multiple keys in the table
error: com.genapi.mobileandroid.model.ClientCiviliteJoin has a foreign key (idClient) that references com.genapi.mobileandroid.model.Client (idCiviliteJoin) but com.genapi.mobileandroid.model.Client does not have a unique index on those columns nor the columns are its primary key
Can anyone suggest me how to have the complete Client object?
Thanks