2

I've already seen a threads similar to this, but none of them answers my question.


So let's say we have two classes:

  • Lesson
  • Break

Both of them are implementing SchoolEntity interface, which has the following methods:

  1. Date getStartDate()
  2. Date getEndDate()

Break class does nothing beside implementing this interface, but Lesson has additional fields and getters, like:

  1. subject
  2. room
  3. teacher, etc.

Now I want to save instance of SchoolEntity using Android's Room Persistence Library.


This is the List I want to be saved:

List<SchoolEntity> timetable:
 1. Lesson@13da43:
    - Date startDate = Date@ha94j8
    - Date endDate = Date@9kf8kf
    - String subject = "math"
    - String room = "124"
    - String teacher = "Joseph Fold"
 2. Break@j7gfsd:
    - Date startDate = Date@738291
    - Date endDate = Date@fj8fjn

And I want it to be saved in SQL Database like this:

+----------------------+---------------------+--------+---------+------+-------------+
| start_date           | end_date            | type   | subject | room | teacher     |
+----------------------+---------------------+--------+---------+------+-------------+
| 2019.01.12 08:00:00  | 2019.01.12 08:45:00 | LESSON | math    | 124  | Joseph Fold |
| 2019.01.12 08:45:00  | 2019.01.12 08:55:00 | BREAK  | NULL    | NULL | NULL        |
+----------------------+---------------------+--------+---------+------+-------------+

Note: start_date is primary key

And finally I want to be able to retrive saved information as:

LiveData<List<SchoolEntity>> timetable

My question is:

Am I able to do this using Android's Room Persistence Library? If so, how to achieve it? If no what should I use instead?


My language is Java, but Kotlin will also make me.

Gieted
  • 835
  • 6
  • 21
  • `start_date` as primary key is doomed to fail... so what if you have more than 1 class, starting at 8:00 am? – Martin Zeitler Jan 12 '19 at 15:59
  • It's no how school lessons work. One lesson have to end to another be started. – Gieted Jan 12 '19 at 16:02
  • a school commonly has more than 1 single class starting at 8:00 am ...most likely in different rooms. while actually, anything that is not a lesson is a break; therefore it's also pointless to store the breaks. using a timestamp as primary key prevents more than 1 single class starting at 8:00 am in different rooms. you'd rather need classes `Lesson`, `Teacher`, `Subject`, `Room` (which may collide with the `Room` namespace). it might also be of interest, who even attends these classes. – Martin Zeitler Jan 12 '19 at 19:43
  • You get me wrong. It's not a school menagment system, but timetable menager app for students. Each student has his own timetable, where lessons can't collide with each other. Also having seperate classes for each lesson information is pointless, as it will be used only to be displayed on a screen – Gieted Jan 12 '19 at 20:05
  • If you are open to trying out a different ORM, check out JDXA ORM for Android. JDXA supports inheritance where the objects of the class hierarchy may be stored either in one table cumulatively (as shown in your question) or in different tables for different classes. Disclaimer: I am the architect for JDXA. – Damodar Periwal Jan 29 '19 at 01:48
  • Yeah, I will try it for sure – Gieted Jan 30 '19 at 08:44
  • @PawełKiełb, Great. Please let me know if you have any questions about JDXA. – Damodar Periwal Jan 31 '19 at 18:20

0 Answers0