Java DAO implementation setObjects

188 Views Asked by At

There is such problem. enter image description here

I have some entity classes Appartment, Landlord etc. Now I'm creating ApartmentDaoImpl, that is DAO implementation for Apartment. Everything is clear with primitive types, I can get landlordId.

The question is - how to set Landlord field by his id?

This doesn't work because getId() method is non-static:

Apartment apartment = new Apartment(id);
            apartment.setRooms(rooms);
            apartment.setDescription(description);
            apartment.setFree(free);
            apartment.setPrice(price);
            apartment.setLastUpdatedPrice(lastUpdatedPrice);
            apartment.setLandlord(Landlord.getId());
1

There are 1 best solutions below

3
On BEST ANSWER

First, you have to decalre Landlord Object and set its properties, e.g.:

Landlord landlord = new Landlord();
landlord.setName("foo");
landlord.setEmail("foo@bar");
etc.

Alternatively, you can get it from DB, if the record already exists:

Landlord landlord = dataAccess.getLandlordByName("hereGoesTheName");

Of course, in the latter case, you need to implement a proper DataAccess class and access its method by dataAccess Object.

Only then you can assign it to the Apartment Entity:

apartment.setLandlord(landlord);

Aside from that, it seems to me that you don't really grasp basic concepts of OOP. Nothing's wrong with that, but the problem you are trying solve is quite an advanced one.