Jenej Fjrjdn
Jenej Fjrjdn

Reputation: 347

Room @Relation with two foreign keys

I have a three entities:

enter image description here

Lets say I wanna receive the Car with the Owner and Brand as with normal SELECT I only receive the ids of my foreign keys.

My approach was using @Relation:

public class CarWithPersonAndBrand {

    @Embedded Person person;
    @Relation(parentColumn =  "id", entityColumn = "brandId",  entity = Brand.class)
    Car brand;
    
    @Relation(parentColumn =  "id", entityColumn = "personId",  entity = Person.class)
    Car car; //this would not make anysence???
}

However using the relation annotation, I receive the Object where the foreignkeys are annoted, however I want other way around. Rather than receiving two car objects, which dont make sence my suggest was receiving brand and person object using the @Relation anotation, would this be possible?

Upvotes: 0

Views: 512

Answers (1)

MikeT
MikeT

Reputation: 56953

Consider that you want CarWithPersonAndBrand

So you want the Car with the respective(related) Person and with the respective Brand.

So you get the Car from the Car table and the related person and the related brand from the respective related tables.

So you could use:-

class CarWithPersonAndBrand {

    @Embedded
    Car  car;
    @Relation(entity = Person.class,parentColumn = "idPerson",entityColumn = "personId")
    Person person;
    @Relation(entity = Brand.class,parentColumn = "idBrand",entityColumn = "brandId")
    Brand brand;
}

This was built using you diagram showing that the Car table has columns idPerson and idBrand and that your CarWithPersonAndBrand class indicates that the Person table has the column personId and that the Brand table has the column brandId (each uniquely identifying the respective row).

With the following Dao :-

@Transaction
@Query("SELECT * FROM car")
List<CarWithPersonAndBrand> getAllCarsWithPersonAndBrand();

And the following in an activity :-

public class MainActivity extends AppCompatActivity {

    MotorDatabase db;
    AllDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = MotorDatabase.getInstance(this);
        dao = db.getAllDao();

        Person p1 = new Person("Mary");
        Person p2 = new Person("Fred");
        Brand b1 = new Brand("Ford");
        Brand b2 = new Brand("Chevrolet");


        p1.personId = dao.insert(p1);
        p2.personId = dao.insert(p2);
        b1.brandId = dao.insert(b1);
        b2.brandId = dao.insert(b2);

        Car c1 = new Car("MyCar",p1.personId,b1.brandId);
        Car c2 = new Car("TheirCar",p2.personId,b1.brandId);
        dao.insert(c1);
        dao.insert(c2);
        List<CarWithPersonAndBrand> carsList = dao.getAllCarsWithPersonAndBrand();

        for (CarWithPersonAndBrand c: carsList) {
            Log.d("CARINFO","Car is " + c.car.carName + " owner is " + c.person.personName + " brand is " + c.brand.brandName);
        }
    }
}

Then the result output to the log of the above is :-

D/CARINFO: Car is MyCar owner is Mary brand is Ford
D/CARINFO: Car is TheirCar owner is Fred brand is Ford

Upvotes: 1

Related Questions