Recently began to deal with android development. I just can’t understand how using Room to save such an object in the database:

@Entity(tableName = "test_class") public class TestClass { @Entity(tableName = "inner_one") public static class InnerClassOne { @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") private long idInnerOne; @ColumnInfo(name = "field") private double field; // getters and setters } @Entity(tableName = "inner_two") public static class InnerClassTwo { @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") private long idInnerTwo; @ColumnInfo(name = "str") private String str; // getters and setters } @Entity(tableName = "inner_tree") public static class InnerClassTree { @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") private long idInnerTree; @ColumnInfo(name = "id_test_class") @ForeignKey(entity = TestClass.class, parentColumns = "id", childColumns = "id_test_class", onDelete = ForeignKey.CASCADE, onUpdate = ForeignKey.CASCADE) long idTest; @ColumnInfo(name = "ser") private int ser; @ColumnInfo(name = "vid") private int vid; // getters and setters } @PrimaryKey(autoGenerate = true) private long id; @ColumnInfo(name = "id_one") @ForeignKey(entity = InnerClassOne.class, parentColumns = "id", childColumns = "id_one", onDelete = ForeignKey.CASCADE, onUpdate = ForeignKey.CASCADE) private long idOne; @ColumnInfo(name = "id_two") @ForeignKey(entity = InnerClassTwo.class, parentColumns = "id", childColumns = "id_two", onDelete = ForeignKey.CASCADE, onUpdate = ForeignKey.CASCADE) private long idTwo; private InnerClassOne innerOne; private InnerClassTwo innerTwo; private List<InnerClassTree> lst; // getters and setters } 

What annotations should I write for the fields innerOne, innerTwo and lst? And what to register in Dao?

  • I can’t do it at all and, for example, it’s hard for me to understand why someone would need to do this. The fact is that each @Entity is a table, how do you present yourself a table in a table in SQL format? You may need @Embedded classes (the "nested objects" section) are needed - pavlofff
  • @pavlofff is not, nested objects are not needed, just need to save all the Entity in their tables. While in Dao I registered methods for saving each Entity, which I call sequentially - nulll
  • in SQL there is no such structure as a table of tables and the fact that it is impossible to implement in your code. Maybe you better add to the question what kind of task you need to solve, and not your [wrong] idea of ​​its solution. - pavlofff
  • Yes, I do not need a table of tables. It is necessary that each Entity lay in a separate table, and in the main table (for which the Entity TestClass is responsible) lay the foreign keys on the records of all nested Entity. Made it through separate methods in Dao, which I invoke sequentially. Accordingly, in Entity TestClass Ignore added annotation to the fields of nested Entity. - nulll
  • Trying to save JSON - nulll to the database

0