Question

I am designing some "blocks" of data in JSF and I am getting my data from a Java bean per block. My problem is that my blocks get their data from more than one table. For example:

A block that describes personal data of a customer consists of:

  • customer name (in table customer)
  • customer surname (in table customer)
  • customer phone (in table customer)
  • customer address (in table address)
  • customer company (in table company)
  • customer phone at work (in table company)

I will have to access 3 separate tables in order to fill this block. Rather than constructing 3 different DAOs (one for each table), isn't it better to construct one DAO per block? I understand that this way has a disadvantage regarding consistency, because if one table will be accessed by more than one DAOs, each change of the table will have to be applied to every one of them. But my code will be much more understandable according to my specific business logic. What are other downsides here? Is it generally advised to create DAOs with access to many tables?

Was it helpful?

Solution

My solution to this problem is implementing a new method called getCustomerWithAddressAndCompanyInfo (or something else shorter) in CustomerDAO. This method runs a single query joining customer, address and company tables and returns the result by a single access to the database.

If you choose accessing 3 DAOs, you have to execute 3 distinct queries on the database which may create a performance issue. As you said, this can cause an inconsistency also.

Another approach may be collecting such complex query methods in a separate class like CustomerQueries rather than implementing them in existing DAO classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top