5

I am creating an application with database access. I do not use JPA nor JooQ or other frameworks for reasons (and do not want to. Also this is not important for my question). So I use JDBC and write plain sql.

My model looks like this:

public class Contact{
  AddressBook addressBok;
  String name;
  ...
}

I now created 2 DAOs, one for Contact and AddressBook. My Contact table has a foreign key to the AddressBook table (address_book_id). I have a Service Class (e.g. ContactService) which would read each object using the corrsponding DAO and combine it to one Contact.

The Problem now is: I have the address_book_id in the ResultSet in the ContactDAO. How do I pass it over to the service class which then reads the correspondig AddressBook using the AddressBookDAO? As the model is shared, it is not a good solution to put a String addressBookId to Contact as clients using this model may do not know anything about the database.

I know these questions, but there is no answer on how to do it: Using DAOs with composite Objects Can a DAO call DAO?

homedom
  • 308
  • 2
  • 8

2 Answers2

0

The best practice is to use POJO domain object per each table where you can save your relationship fields like address_book_id. So you will have tree independent classes Contact, Address, AddressBook and independent DAO ContractDAO, AddressDAO, AddressBookDAO. Your ContactService will manipulate with these 6 objects to load, save, modify related data.

SanyaLuc
  • 116
  • 5
  • Yes of course I can add the field addressbookid to my contact class. But wouldn't that be bad object design? I would rather put the adressbook object into the contact class. I do not want to make my model "dirty" just because of the database strategy?! – homedom May 23 '15 at 19:29
  • Yes why not, you can use reference to addressbook object and use getAddressBookId() to get id via addressbook object. Either make reference to your object or only to id depends on your needs. If you want to load all cascading structure it is better to bind a whole object. – SanyaLuc May 23 '15 at 19:59
  • I did not get your answer. I have an object Contact which contains an object addressbook. Now I have a method which should read both objects and set the addressbook in the contact object. This method uses both daos. But after reading contact from contactdao I do not have any reference to the addresabookid because this list not an attribute of my contact class as this is IMHO bad design. So how do I pass the addressbookid from the contactdao to the method reading both objects? – homedom May 23 '15 at 20:06
0

you can use the decorator pattern to wrap the entity(model class) that you want to add a foreign key to it, like this: enter image description here

how to use: for example: save(insert into db) a contact (here where I found the problem last time, so i fixed it by this solution)--> problem:how to insert a contact without knowing the fk address_book_id, in the args of save

    public class ContactDAO extends DAO<Contact>{
    // ....
    
    @Override
    public int save(IEntity contact){
        ForeignKeyWrapper ctFk = (ForeignKeyWrapper)contact;
        int address_book_id = ctFk.getFK();
        Contact ct = (Contact)ctFk.getEntity();
        String name = ct.getName();
        // retrieve other fields of contact here
    
        //use some sql to insert the contact in the db, now you have also the fk
        String insertQuery = "INSERT INTO CONTACT(name,..other fields of contact.. , address_book_id) VALUES("+
                     name + "," +
                    /*other fields*/
                    address_book_id + ")";
        //execute it here and fetch the id of the inserted row
    
    }
    
    // ....
    }
    
    public class clientClass{
        //....
            public static void main(String[] args) {
            IEntity contact = new Contact(/* init fields*/);
            IEntity addressBook = new AddressBook(/* init fields*/);
            ForeignKeyWrapper ctFKW = new ForeignKeyWrapper(contact);
            //link contact to addressBook (e.g: when creating a contact to insert it into the db)
            ctFKW.setFK(addressBook.getId());
            ContactDAO ctDAO = new ContactDAO(/*connection*/);
            ctDAO.save(ctFKW);
        }
        //....
    
    }

you can add a builder class to build your contact objects and link their foreign keys to addressBook objects Ids, to encapsulate the building logic, and simplify the process

Fateh
  • 3
  • 7