question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi, Is there any support for many to many relations ? If not, How would you implement it ?

I thought about something like that: With doctors having many patients and patients having many doctors, all through an appointment

Doctor.java

    @Table
    public class Doctor extends BaseModel {

        @Column(columnType = Column.PRIMARY_KEY)
        public  int               id;
        public  List<Appointment> appointments;
        private List<Patient>     patients;

        public Doctor() {
            super();
        }

        public List<Appointment> getAppointments() {
            if (appointments == null) {
                appointments = Select.all(Appointment.class, Condition.column(Appointment$Table.DOCTORID).is(id));
            }
            return appointments;
        }

        public List<Patient> getPatients() {
            if (patients == null) {
                // patients = { Select.all Patient INNER JOIN Appointments ON Patient.id = patientId }
            }
            return patients;
        }
    }

Patient.java

    @Table
    public class Patient extends BaseModel {

        @Column(columnType = Column.PRIMARY_KEY)
        public  int               id;
        public  List<Appointment> appointments;
        private List<Doctor>      doctors;

        public Patient() {
            super();
        }

        public List<Appointment> getAppointments() {
            if (appointments == null) {
                appointments = Select.all(Appointment.class, Condition.column(Appointment$Table.PATIENTID).is(id));
            }
            return appointments;
        }

        public List<Doctor> getDoctor() {
            if (doctors == null) {
                // patients = { Select.all Patient INNER JOIN Appointments ON Patient.id = patientId }
            }
            return doctors;
        }
    }

Appointment.java

    @Table
    public class Appointment extends BaseModel {

        @Column(columnType = Column.PRIMARY_KEY_AUTO_INCREMENT)
        public int id;
        public int doctorId;
        public int patientId;

        @Column(columnType = Column.FOREIGN_KEY,
                references = { @ForeignKeyReference(columnType = Appointment.class,
                        columnName = "appointment", foreignColumnName = "id") })
        public Patient patient;
        @Column(columnType = Column.FOREIGN_KEY,
                references = { @ForeignKeyReference(columnType = Appointment.class,
                        columnName = "appointment", foreignColumnName = "id") })
        public Doctor  doctor;

        public Appointment() {
            super();
        }

        public Doctor getDoctor() {
            return doctor;
        }

        public Patient getPatient() {
            return patient;
        }

    }

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:25 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
damsoncommented, Jan 22, 2015

The issue of the logs was coming from the android studio debugger. So it’s not really an issue but rather “latency”.

And for the weird result, I was simply executing a wrong request. So here is the fixed request to retrieve the linked patients.

// public List<Patient> getPatients()

 patients = new Select(Patient$Table.TABLE_NAME + ".*")
                    .from(Patient.class)
                    .join(Appointment.class, Join.JoinType.INNER)
                    .on(Condition.column(Appointment$Table.TABLE_NAME + "." + Appointment$Table.PATIENTID)
                            .is(Patient$Table.TABLE_NAME + "." + Patient$Table.ID))
                    .where(Condition.column(Appointments$Table.DOCTORID).is(this.id)) // doctor.id
                    .queryList();

Read more comments on GitHub >

github_iconTop Results From Across the Web

Many-to-many (data model) - Wikipedia
In systems analysis, a many-to-many relationship is a type of cardinality that refers to the relationship between two entities, say, A and B, ......
Read more >
Many-to-many relationships
A many-to-many relationship occurs when multiple records in a table are associated with multiple records in another table. For example, a many-to-many ......
Read more >
What Is a Many-to-Many Relationship in a Database? An ...
A many-to-many (or M:N) relationship is one of the three database relationships. The other two are: ... By definition, a many-to-many relationship ...
Read more >
Video: Create many-to-many relationships - Microsoft Support
A many-to-many relationship exists when one or more items in one table can have a relationship to one or more items in another...
Read more >
Many-to-Many Relationship in Database Design - DZone
A many-to-many relationship occurs when multiple records in a table are related to multiple records in another table.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found