Multiple DAOs with dependencies (foreign key)

I am creating an application with database access. I am not using JPA or JooQ or other frameworks for reasons (and I don’t want to. Also not important for my question). So I am using JDBC and writing simple sql.

My model looks like this:

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

      

Now I have created 2 DAOs, one for Contact and AddressBook. The My Contact table has a foreign key in the AddressBook table (address_book_id). I have a service class (like ContactService) that will read each object using the appropriate DAO and combine it with a single contact.

Now the problem: I have the book_id address in the ResultSet in the ContactDAO. How do I pass it to the service class, which then reads the appropriate address book using the AddressBookDAO? Since the model is generic, this is not a good solution to String addressBookId

contact, as customers using this model may not know anything about the database.

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

+3


source to share


1 answer


The best practice is to use a POJO domain object for each table where you can store the relationship fields, for example address_book_id

. Thus, you will drevostoyaschie classes Contact

, Address

, AddressBook

and independent the DAO ContractDAO

, AddressDAO

, AddressBookDAO

. Yours ContactService

will work with these 6 objects to load, save, modify related data.



0


source







All Articles