I am new to the world of DAO. I have 3 tables.
- Consumer
- Bill
- Receipt
Consumer table contains following fields
- consumer_id
- consumer_name
- address
Bill table contains
- bill_number
- bill_date
- consumer_id
- bill_amount
Receipt table contains
- receipt_no
- pay_date
- paid_amount
- consumer_id
Bill table has a many to one relationship with Consumer table, Receipt table also has a many to one relationship with Consumer table.
Currently I have created three classes
- Consumer
- Bill
- Receipt
and created DAOs for them like ConsumerDAO, BillDAO, ReceiptDAO. They contains basic CRUD operations.
Now I want a listing of a consumer involving data from these three tables. I am using a JOIN SQL query for the same. It is as follows:
SELECT c.consumer_id,
c.consumer_name,
c.address,
r.receipt_no,
r.pay_date,
r.paid_amount,
b.bill_date,
b.bill_number,
b.bill_amount
FROM consumer c
LEFT OUTER JOIN
(SELECT r.consumer_id,
r.pay_date,
r.receipt_number,
r.paid_amount,
ROW_NUMBER() OVER (PARTITION BY r.consumer_id
ORDER BY r.pay_date) AS rank
FROM receipt r) r ON (c.consumer_id = r.consumer_id
AND r.rank = 1)
LEFT OUTER JOIN
(SELECT b.consumer_id,
b.bill_number,
b.bill_date,
b.bill_amount,
ROW_NUMBER() OVER (PARTITION BY b.consumer_id
ORDER BY b.bill_date) AS rank
FROM bill b) b ON (c.consumer_id = b.consumer_id
AND b.rank = 1)";
I was wondering in which DAO I should place this query ? I thought to add a Bill field and a Receipt field in the Consumer class and add a getCurrentBillAndReceipt() method in the ConsumerDAO. Is it the right way to do ?
I read the following question:
how to create a DAO for join tables
and some more, but I was not able to get the idea.