<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Naveen Niraula on Medium]]></title>
        <description><![CDATA[Stories by Naveen Niraula on Medium]]></description>
        <link>https://medium.com/@naveenniraula?source=rss-60683da9fb5d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*4Hg7SmeCWve4nWlo.</url>
            <title>Stories by Naveen Niraula on Medium</title>
            <link>https://medium.com/@naveenniraula?source=rss-60683da9fb5d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 13:35:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@naveenniraula/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Room and many-to-many relationship.]]></title>
            <link>https://medium.com/@naveenniraula/room-and-many-to-many-relationship-e30ec47c233d?source=rss-60683da9fb5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e30ec47c233d</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[room]]></category>
            <dc:creator><![CDATA[Naveen Niraula]]></dc:creator>
            <pubDate>Fri, 05 Oct 2018 16:15:56 GMT</pubDate>
            <atom:updated>2018-10-05T16:15:56.862Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re here, the chances are you’re in the same dilemma as I once was. The great Room library and you guessed it: the mighty many-to-many relationship. Let me elaborate the problem for you if you have not faced this problem yet (or are just curious what the hell am I writing ?).</p><p>Let us consider this scenario: <strong>Student</strong>, <strong>Class</strong>, <strong>Enrollment</strong>. These three are my tables where: One student can enroll in one or more classes and one class can contain one or more than one student. The enrollment table just takes care of mapping the students to their classes and vice-versa ( what would we do without the foreign keys #lol).</p><p>However, there is a catch, Room does not return related table entries automatically. Before you tell me it does, let me tell you this: I KNOW. But as someone who is just starting out solving these kinds of problem. I’ve found the available tutorials either: <em>moderately complex to implement ( because half of the tutorial doesn’t even exists )</em> <strong>or</strong> <em>it was not what I wanted to do</em>.</p><p><em>Oh, and also if you do not already know the </em><strong><em>Entity </em></strong><em>and </em><strong><em>POJO </em></strong><em>are not the same thing. *sarcasm* I found that out the hard way, thanks to my disciplined way of following documentation *sarcasm*. Please take note of this.</em></p><p>Our entity classes are as follows:</p><pre>/**<br>* Student Model <br>*/<br>@Entity<br>public class Student {<br>    @PrimaryKey(autoGenerate = true)<br>    private int studentId;<br>    private String studentName;</pre><pre>    // add getter and setters here<br>}</pre><pre>/**<br>* Class Model <br>*/<br>@Entity<br>public class ClassModel {<br>    @PrimaryKey(autoGenerate = true)<br>    private int classId;<br>    private String className;</pre><pre>    // add getter and setters here<br>}</pre><pre>/**<br>* Enroll Model <br>*/<br>@Entity<br>public class Enroll {<br>    @PrimaryKey(autoGenerate = true)<br>    private int enrollId;<br>    private String enrollClassFk;<br>    private String enrollStudentFk;</pre><pre>    // add getter and setters here<br>}</pre><p>After the <strong>Entity </strong>classes are set up. We have to specify the relationship between <strong>Enroll</strong>, <strong>Class</strong> and <strong>Student</strong>. Next we are doing exactly that.</p><pre>/**<br>* Enroll Model <br>*/<br>@Entity(foreignKeys = {<br>  @ForeignKey(entity = ClassModel.class, parentColumns = &quot;classId&quot;, childColumns = &quot;enrollClassFk&quot;, onDelete = CASCADE),<br>  @ForeignKey(entity = Student.class, parentColumns = &quot;studentId&quot;, childColumns = &quot;enrollStudentFk&quot;, onDelete = CASCADE)})<br>public class Enroll {<br>    @PrimaryKey(autoGenerate = true)<br>    private int enrollId;<br>    private String enrollClassFk;<br>    private String enrollStudentFk;<br>    private String addAttribOne;</pre><pre>// add getter and setters here<br>}</pre><p>We have added two foreign key constraints which obviously enough maps the values to its respected row(s). After this is done we we have to write our fetch query using join ( not really ? ). But first let us create a <strong>POJO </strong>and not the <strong>Entity</strong>.</p><pre>public class StudentClassMappedPojo {<br>    private int studentId;<br>    private String studentName;<br>    private int classId;<br>    private String className;<br>    private int enrollId;<br>    private String enrollClassFk;<br>    private String enrollStudentFk;</pre><pre>    // set getters and setter here<br>}</pre><p>As you can see i’ve copied every member variable from 3 classes and that is because i’m lazy. But seriously, create the pojo with the only ones you need. By this time, you might probably have guessed what i’m going to do next. But if you have not follow along. In our <strong>Dao </strong>let us write the fetch query.</p><pre>@Dao<br>public class EnrollDao {<br>    <br>    @Query(&quot;SELECT * FROM Student s, Class c, Enroll e WHERE e.enrollClassId = c.classId AND e.enrollStudentId = s.studentId &quot;)<br>    List&lt;StudentClassMappedPojo&gt; getAll();<br>}</pre><p>Yes that is it now if you execute the query evey related data will be fetched from the database.</p><p><em>NOTE: I am just starting out with android development. So please take this with a pinch of salt on if this is the correct way to do it. There might be better and more efficient way to do this. But this is how I currently am tackling it, the problem. Also, this is my first post here so… well… happy coding.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e30ec47c233d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>