Insert with relation in the Room

0

I'm not really sure how Relation Room works, I'm trying to do an insert of two related tables, many ORMs can do this, insert Person and Books into the table, without needing to set the id of books (which is related to Person).

I was able to get Room to return Person and Books related to person, but I'm not able to do an insert (if any), I did not find information if it has a form or not. >

@Entity(tableName = "books")
public class Book {

    @PrimaryKey(autoGenerate = true)
    private long id;
    @ColumnInfo(name = "person_id")
    private int personId;
    private String name;
    private String author;
    //@ColumnInfo(name = "release_date")
    @Ignore
    private Date releaseDate;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getPersonId() {
        return personId;
    }

    public void setPersonId(int personId) {
        this.personId = personId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }
}

@Entity
public class Person {

    @PrimaryKey(autoGenerate = true)
    private long id;
    @ColumnInfo(name = "first_name")
    private String firstName;
    @ColumnInfo(name = "last_name")
    private String lastName;
    private int age;
    private String email;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

@Dao
public interface PersonBookDao {

    @Query("SELECT * FROM person")
    List<PersonWithBook> loadPersonsAndBooks();

    //@Insert
    //void insert(PersonWithBook personWithBook);

}

Then I normally enter using the Book and Person dao interfaces, I tried to put the @Insert annotation inside the PersonBookDao class, but the error, stating that it is mandatory to be Entity.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AppDatabase db = AppDatabase.getAppDatabase(this);

        Person person = new Person();
        person.setFirstName("Aaaaaa");
        person.setLastName("Bbbbbbb");
        person.setAge(26);
        person.setEmail("[email protected]");

        db.personDao().insert(person);

        List<Book> bookList = new ArrayList<>();
        Book book = new Book();
        book.setAuthor("AAAAAAA");
        book.setName("Nnnnnnn");
        book.setPersonId(1);
        bookList.add(book);

        PersonWithBook personWithBook = new PersonWithBook();
        personWithBook.person = person;
        personWithBook.bookList = bookList;

        db.bookDao().insert(book);

        //List<PersonWithBook> pb = db.personBookDao().loadPersonsAndBooks();
        //Log.d("aaaaaaaa", pb.get(0).person.getEmail());
    }
}

Everything leads me to believe that in the Room it is not possible ... But I want to be sure if it does not really work, or if there is any better way than inserting the Person class ID.

    
asked by anonymous 18.01.2018 / 17:30

0 answers