如何在会议室数据库中创建实体继承?

时间:2019-06-07 14:03:31

标签: java android android-room

早上好,我正在使用SQLite数据库,然后我已更改为Room。我有一个超类“ Person”和两个子类“ Femme”和“ Enfant”。 如何在Room中创建两个子类的表。

Person.java

public class Personne {

private  int idP;
private String nom,prenom;
private int age, poids;


public Personne(String nom, String prenom, int age, int poids) {

    this.nom = nom;
    this.prenom = prenom;
    this.age = age;
    this.poids = poids;

}

public Personne(String nom, String prenom) {
    this.nom = nom;
    this.prenom = prenom;

}

Femme.java

  public class Femme extends Personne { 

   private String pseudo,mdp,grpSang;

public Femme(String nom, String prenom, int age, int poid, String pseudo, 
 String mdp, String grpSang) {
    super(nom, prenom, age, poid);
    this.pseudo = pseudo;
    this.mdp = mdp;
    this.grpSang = grpSang;
}
}

Enfant.java

 public class Enfant extends Personne {

     private float taille;
     private Sexe sexe;
      private Date DN;

public Enfant(String nom, String prenom, int age, int poid, int taille, 
 Sexe sexe) {
    super(nom, prenom, age, poid);
    this.taille = taille;
    this.sexe = sexe;
}
}

1 个答案:

答案 0 :(得分:0)

下午好,伙计。让我们开始在Persoone对象中创建两个变量。您的新[/d/]+如下所示:

Personne.java

在上面的代码块中,我添加了两个子类的列表,但@忽略它们。因为我不需要它们,但是。您实际上可以删除它们。

让我们进入您的子类。

@Entity (tableName = "yourPersonneTableName")
public class Personne {

@PrimaryKey
private  int idP;
private String nom,prenom;
private int age, poids;
@Ignore
private List<Femme> femmeList;
@Ignore
private List<Enfant> enfant;

// setters and getters here

}

}

就是这样。您可以将数据部分添加到所有这些表中。但是您可以通过@Entity (tableName = "yourEnfantTableName") public class Enfant{ @PrimaryKey private long newId; // you can have newId here or change one of your original parameters into PrimaryKey private int idP; // you can manipulate through this id of your Personne table there. private float taille; private Sexe sexe; private Date DN; // setters and getters here } @Entity (tableName = "yourFemmeTableName") public class Femme{ @PrimaryKey private long newId // you can have newId here or your can change one of your parameter into PrimaryKey private int idP; // you can manipulate through this id of your Personne table there. private String pseudo,mdp,grpSang; 随意使用它们。

另一种类型是使用@TypeConverters

使用您的Enfant类的一个示例

idP

}

但是您的Personne.class会有所更改。

这是您的资源。

public class EnfantToStringConverter {

@TypeConverter
public String imageToString(List<Enfant> enfant){
    return new Gson().toJson(enfant);
}

@TypeConverter
public List<ImagesVO> stringToImage(String strGson){
    return new Gson().fromJson(strGson, new TypeToken<List<Enfant>>(){}.getType());
}

无论如何,您只需要在这里阅读一点。

RoomTypeConverter

RoomTypeConverters

Ignore