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...
thank you bro!
simple and crisp!
no unnecessary things and straight to the point.
Good tutorial thanks, I hope you to add Paging with Room in the next videos
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.
instead of return a list from the query you could return a livedata too?
Thank you very much Philip for your wonderful advertising.....
Helpful video about room relashionships , thank you
thanks man, your tutorials are amazing!
Awesome, great work Phillip. Thank you. 🙏
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!
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..
Perfect!!
This is going to cool playlist. Thank you.
Glad you like it!
Thank you, man!
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?
Thank you very much!
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
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)
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 ♥.
Thank you so much ❤️🥰🥰
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?
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.
@@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?
@@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
@@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.
@@nikhiltambe7204 Yes you are right. Returning a single SchoolWithStudents in the Dao works for this example
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?
Hi, He explain this on the first video of playlist. It makes easier to understand the theory.
Thank you, Bro!)
Love You Bro!
Superb
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.
give an example for Java?
What to do if I want only specific items of the child entity?
If you use id fields for the primary keys would you still use schoolName as both parentColumn & Entity column?
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
Hey, do you add the relations entities to the database list of entities in the database class?
No, these are not entities, just helper classes for room
@@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?
GREAT VIDEO
What does transaction mean? Why do you use it, I can't understand explain me please
Why is the student name primary key? There can be multiple students with the same name in the same school.
thanks for this video! :)
You're so welcome!
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.
What if If the parent had a list of Strings instead of an Object? How would you do implement the relationship (annotation)?
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.
Thanks
Hi, I ve a question. Why don't you annotate the data class with @Entity?
Sir please make video for deleting data in room db which having relations locked
Hi Philipp , Could you make video or add working with WorkManager and Jetpack Pagination.
did you find the solution for your question?
It would hit whoever put doesn't like
if we delete an Object of SchoolWithStudents , will all students in this school be deleted automatically or not?
And what if I had a School without students? How to solve that in the query.
If the director only have a name field, can we just declare it as a direct field of school?
yes
Why Student is not mentioned as Entity class with @Entity annotation?
Why such discrimination with Students? :-D ;-)
Hello Philip, what about inserting school and students at the same time? When the ids are auto increment, this becomes too hard to implement.
Ohhhh sh** 😁
like :)
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...
This is not because of android, that's just how sql works
@@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
@@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
Thanks