One-to-N Relationships - Android Room Database With Multiple Tables

Поделиться
HTML-код
  • Опубликовано: 16 сен 2024
  • In this video you will learn how to handle 1:n relationships with Room.
    ⭐ Get certificates for your future job
    ⭐ Save countless hours of time
    ⭐ 100% money back guarantee for 30 days
    ⭐ Become a professional Android developer now:
    pl-coding.com/...
    Get the source code for this video here:
    github.com/phi...
    Regular Android tutorials on my Instagram: / _philipplackner_
    Checkout my GitHub: github.com/phi...

Комментарии • 68

  • @macheeneraid230
    @macheeneraid230 3 года назад +6

    thank you bro!
    simple and crisp!
    no unnecessary things and straight to the point.

  • @AlaaAlbrg
    @AlaaAlbrg 3 года назад +4

    Good tutorial thanks, I hope you to add Paging with Room in the next videos

  • @joewilk4118
    @joewilk4118 Год назад

    I'm new to Android/Kotlin programming but have years of experience with MSSQL. You briefly mentioned in your first SQL tutorial that id would be used for primary key in the real world. I really wish you had followed through with this for your examples since that is how it really works in practical applications. Other than that you've provided a lot to consider for my next app. The first app was a single table database. Now I'll have to see if this way is better than just using the sqlitehelper for multiple tables.

  • @josema2702
    @josema2702 Год назад

    instead of return a list from the query you could return a livedata too?

  • @lewekandi784
    @lewekandi784 2 года назад

    Thank you very much Philip for your wonderful advertising.....

  • @AlgeriaHere
    @AlgeriaHere 3 года назад

    Helpful video about room relashionships , thank you

  • @emad9149
    @emad9149 Год назад

    thanks man, your tutorials are amazing!

  • @rahulbhavani6160
    @rahulbhavani6160 3 года назад

    Awesome, great work Phillip. Thank you. 🙏

  • @alinemansur3824
    @alinemansur3824 10 месяцев назад

    Teacher, perfect class!
    I would like a professional opinion from you on the database.
    For a large app (Airbnb or Uber), using Room for local persistence may be better suited than pure SQLite, right?
    I ask this because I have no problems writing code using the SQL language.
    Thank you for your tutorials!

  • @johnmarklecias938
    @johnmarklecias938 2 года назад +1

    Do I need to have separate DAOs, repositories, and repository implementations for each of my entities? I have been learning MVVM for only a couple of weeks, so I am not sure what to do..

  • @seeukrainewithme8380
    @seeukrainewithme8380 11 месяцев назад

    Perfect!!

  • @baijusharma6027
    @baijusharma6027 3 года назад

    This is going to cool playlist. Thank you.

  • @GriNAME
    @GriNAME 2 года назад

    Thank you, man!

  • @starlite5097
    @starlite5097 3 года назад +1

    Nice video. I have a question. Why would I declare a 1:N relationship between the tables if I want to retrieve the students that go to school "X"? I can do that in the DAO in a query function directly. Am I missing something?

  • @faizansyed3811
    @faizansyed3811 2 года назад

    Thank you very much!

  • @walterurbina813
    @walterurbina813 2 года назад

    How could you relate 3 tables where the union is one to one?
    A -> B -> C
    For example
    Entities:
    - Student
    - Locker
    - Book (assuming he can only have one book saved)
    The user wants to know which book has in his locker

    • @PhilippLackner
      @PhilippLackner  2 года назад

      Student has lockerId and locker has bookId
      SELECT *
      FROM student s JOIN locker l ON(s.lockerId == l.id) JOIN book b ON(b.id == l.bookId)

  • @qusaiatoon2802
    @qusaiatoon2802 3 года назад +2

    Cool tutorial, but I can not find a way to insert values now after I started dealing with relations.
    Can someone please tell me where to learn how to do that.
    thanks in advance ♥.

  • @meetsoni1938
    @meetsoni1938 Год назад

    Thank you so much ❤️🥰🥰

  • @nikhiltambe7204
    @nikhiltambe7204 3 года назад +1

    Hey Philipp, love your work, i has been of great help.
    Maybe I've missed out something or maybe you will be covering it in the upcoming videos.
    But I had a question though, since school name is primary key, shouldn't the last 2 queries return a single object, rather than a List?

    • @PhilippLackner
      @PhilippLackner  3 года назад

      Why should they return a single object? We query our database and want to get all rows that match the query. For the first query we want to get all schools together with their director and for the second one we get all schools together with their students. Not just a single school.

    • @nikhiltambe7204
      @nikhiltambe7204 3 года назад

      @@PhilippLackner Since school name is a primary key and one school name is mapped to one director through relation, if we query a specific school name, wouldn't it return a single school (if present in table). Which would mean, the List would have only one SchoolAndDirector object as a result, right?

    • @PhilippLackner
      @PhilippLackner  3 года назад +1

      @@nikhiltambe7204 oh you mean in this specific example, yes that's correct sorry. I thought this was a general question. I'm not sure but I think we still need to return a list here since room doesn't know that

    • @nikhiltambe7204
      @nikhiltambe7204 3 года назад +1

      @@PhilippLackner yes. Sorry I should have been more specific. I haven't tried it yet, but will be doing it first thing after your series is done. Thanks for the explanation though. You do it really well and it has helped me learn a lot. Thanks again.

    • @bharatn9160
      @bharatn9160 3 года назад +1

      @@nikhiltambe7204 Yes you are right. Returning a single SchoolWithStudents in the Dao works for this example

  • @АртемОкунев-ж1т
    @АртемОкунев-ж1т 3 года назад +1

    Can you please explain me, why did you make the property "studentName" as PrimaryKey. Can you be sure,that there won't be 2 students with the same name?

    • @rafaelmonoh
      @rafaelmonoh 3 года назад

      Hi, He explain this on the first video of playlist. It makes easier to understand the theory.

  • @mikecortegana9685
    @mikecortegana9685 2 года назад

    Thank you, Bro!)

  • @vamsireddy7560
    @vamsireddy7560 3 года назад

    Love You Bro!

  • @AhsanAli-vz6mg
    @AhsanAli-vz6mg 3 года назад

    Superb

  • @AbdulWahab-kk1hs
    @AbdulWahab-kk1hs 3 года назад

    I think there's a mistake in this video and pls correct me if I'm wrong. getSchoolWithStudents can't return List since we are taking schoolName as a parameter and giving results which contains that schoolName and schoolName can only be one because that is a primary key. No two schools can have the same name.

  • @projectfilm6800
    @projectfilm6800 2 года назад

    give an example for Java?

  • @meetsoni1938
    @meetsoni1938 Год назад

    What to do if I want only specific items of the child entity?

  • @davidozersky412
    @davidozersky412 Год назад

    If you use id fields for the primary keys would you still use schoolName as both parentColumn & Entity column?

  • @shagorjhon7654
    @shagorjhon7654 Год назад

    hello,
    I have a note apps, If any note item has added to favourite, then the favourite activity will show only the note item , that have been added to favorites. I am using room database and there have multiple entities
    please help

  • @VickiMes
    @VickiMes 3 года назад +1

    Hey, do you add the relations entities to the database list of entities in the database class?

    • @PhilippLackner
      @PhilippLackner  3 года назад +1

      No, these are not entities, just helper classes for room

    • @VickiMes
      @VickiMes 3 года назад

      @@PhilippLackner Thanks, quick question. Can I use one helper class in another helper class? i.e I have an entity with a collection of another entity which has its own sub collection of another entities. Can the helper class of the first entity reference the helper class of the second entity?

  • @jamesedwards7966
    @jamesedwards7966 3 года назад

    GREAT VIDEO

  • @БекуланОморов
    @БекуланОморов 3 года назад

    What does transaction mean? Why do you use it, I can't understand explain me please

  • @jurajbuljevic1977
    @jurajbuljevic1977 2 года назад

    Why is the student name primary key? There can be multiple students with the same name in the same school.

  • @sinaa7562
    @sinaa7562 2 года назад

    thanks for this video! :)

  • @samirshrestha5252
    @samirshrestha5252 2 года назад

    which is the better way to update single field of table in one to many and many to many relationship? Hope to get positive response soon.

  • @Rafael-hk9pg
    @Rafael-hk9pg 3 года назад

    What if If the parent had a list of Strings instead of an Object? How would you do implement the relationship (annotation)?

  • @shivayogihiremath6074
    @shivayogihiremath6074 3 года назад

    Hi , I am looking for the way to sync the room db with server... could you help us.? its is like i am getting data from server and storing it in roomdb if any table changes or new values inserted room db data should be update to server.

  • @amineayachi335
    @amineayachi335 3 года назад

    Thanks

  • @alberthe90
    @alberthe90 3 года назад

    Hi, I ve a question. Why don't you annotate the data class with @Entity?

  • @techbrainz
    @techbrainz 3 года назад

    Sir please make video for deleting data in room db which having relations locked

  • @jafartemirov7812
    @jafartemirov7812 3 года назад

    Hi Philipp , Could you make video or add working with WorkManager and Jetpack Pagination.

    • @amlivinginhell
      @amlivinginhell 3 года назад

      did you find the solution for your question?

  • @brunodiaz6100
    @brunodiaz6100 3 года назад

    It would hit whoever put doesn't like

  • @abada-s
    @abada-s 2 года назад

    if we delete an Object of SchoolWithStudents , will all students in this school be deleted automatically or not?

  • @rafaelparanhos7435
    @rafaelparanhos7435 3 года назад

    And what if I had a School without students? How to solve that in the query.

  • @enryd...
    @enryd... Год назад

    If the director only have a name field, can we just declare it as a direct field of school?

  • @rahulnirmesh3388
    @rahulnirmesh3388 3 года назад

    Why Student is not mentioned as Entity class with @Entity annotation?
    Why such discrimination with Students? :-D ;-)

  • @resitsahin7787
    @resitsahin7787 2 года назад

    Hello Philip, what about inserting school and students at the same time? When the ids are auto increment, this becomes too hard to implement.

  • @h.r.60
    @h.r.60 3 года назад

    Ohhhh sh** 😁

  • @mauriciopriego1700
    @mauriciopriego1700 2 года назад

    like :)

  • @peshutanpavri1599
    @peshutanpavri1599 2 года назад

    The tutorial is amazing but why does Android have this awful design where you have to have an entire class just to model a relationship? This should be a single annotation like @OneToMany etc...

    • @PhilippLackner
      @PhilippLackner  2 года назад +1

      This is not because of android, that's just how sql works

    • @peshutanpavri1599
      @peshutanpavri1599 2 года назад

      @@PhilippLackner Thanks for getting back to me, I really love your tutorials, but this one aspect is just awful design & I haven't seen it anywhere but in Android,
      I have never seen so much boiler plate code just to model a 1:N relation, in most ORM's we don't have to create multiple classes just to model a relation. I built an ORM for my dissertation and I am able to model this entire thing with a single annotation, without even specifying a single field or any configuration.
      I can't see how all this is necessary. Even if you see JPA Hibernate implementation, Its way simpler and more elegant than this.
      A simple @OneToMany annotation should be enough, the code should be able to pick up the foreign key by referencing the entire entity
      Also having to put school name as a string in the child class is confusing, what if I want the whole school entity? What if I want a different value,
      It would seem more intuitive to have the whole school as an entity

    • @peshutanpavri1599
      @peshutanpavri1599 2 года назад

      @@PhilippLackner I read the documentation, I now realise why its like this, I also saw there is a multi-map option which is simpler for me so I might try this out

  • @hellosagar
    @hellosagar 3 года назад

    Thanks