0

Is it posible to build a common DAO class for different beans/tables? Currently I am creating different DAO classes for each different table like: CustomerDAO, EmployeeDAO, StudentDAO.

davioooh
  • 23,742
  • 39
  • 159
  • 250
Vivek
  • 1

1 Answers1

0

Yes it is possible.

How ?

This is Base DBO class for all your CustomerDBO, EmployeeDBO, StudentDBO.

public class BaseDBO {
}

Now, each of your DBO will be like this,

public class CustomerDBO extends BaseDBO {}
public class EmployeeDBO extends BaseDBO {}
public class StudentDBO extends BaseDBO {}

Write all the common methods in BaseDAO like

public class BaseDAO<T extends BaseDBO> {

    void create(T t){}
    void update(T t){}
    void delete(T t){}
}

Now on specific DAO looks like

CustomerDAO,

public class CustomerDAO extends BaseDAO<CustomerDBO> {
}

Now, for CustomerDAO contains all the methods which are common for DAO.

Similarly for StudentDAO, EmployeeDAO.

Hope this helps !!

Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
  • You actually don't need to have your entities (what you call DBO here) extends a `BaseDBO`. You can have your `BaseDAO`just take `T`. `public class BaseDAO` – Matt Aug 25 '14 at 14:07
  • @Matt, Funda is, If I have common columns like id/creation time, I can use this Base class to define them - not to make DBO to look clumsy. For this particular scenario to define the common DAO, it is not required - I agree to that point. – Vinay Veluri Aug 26 '14 at 04:54