开发者

DAO layer with multiple Value Objects

I开发者_StackOverflow have to design DAO layer,From DAO object i am calling view,it returns to me all the data.Below is the scenario:

When i do customer search,it returns to me his contact details,his transaction details,his address details.

SO finally i am getting all the data ,it refers to individual entities(addressvo,transactionvo and contectvo).

If i loop through result set,here how can i seperate data and assign it to my Value object?

Regards, Chaitu


Don't just dump-in whole code in one DAO. Have Four DAOs. One of the good design is to have one DAO per table. So, you have AddressDAO, TrandactionDAO, ContactDAO and UserDAO. If you should have four tables,

addresses
  a_id
  user_id (FK)
transactions
  t_id
  user_id (FK)
contacts
  c_id
  user_id (FK)
users
  user_id (PK)

Now, you write your UserDAO.searchUsers(...) this method returns the UserVO with other VOs as it's attribute:

 List<UserVO> UserDAO.searchUsers(...){

     //search query returns user_ids
     //populate the UserVOs

     //for each user_id
        userList.add(UserDAO.getUserById(user_id));

     //return userList
}

UserVO getUserById(...){
     //get UserVO from users tables
     UserVO uvo = ...;

     //populate rest of the attributes
     uvo.setAddress(AddressDAO.getAddress(user_id));
     uvo.setTransactions(TransactionDAO.getTransactions(user_id));
     uvo.setContacts(ContactDAO.getContacts(user_id));

     return uvo;
}

Update after comment

But here i am getting all the information in one resultset object.Here i have one view,it returns to me whole object

Yeah, this is standard dilemma while mapping relational database to objects. On one hand you have all the data in one query but messy object creation (the one you want); on other, you have cleaner Java code, but requires multiple DB hits (the one that I've suggested above).

If you just want to populate the objects, it's simple (yet messy). I guess you might have one-to-many mapping between users and contants; users and transactions. In that case, you will have multiple rows per user-id. Follow the steps below:

while(resultSet.next()){
  long userId = resultSet.getLong("user_id");
  UserVO uvo = //populate the userVO with users table data
  do{
    //create List of *unique* contactVO, transactionVO and addressVO set them to userVO
  }while(resultSet.next() && resultSet.getLong("user_id") == userId);

  //check if resultSet is over? else user_id changes move back one row
  if(!resultSet.isAfterLast())
    resultSet.previous();
  else
    break; //resultset exhausted
}

As you see, you are essentially weeding the components off resultset. And it is messy. I would rather suggest against it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜