DAO涉及JOIN查询

kau*_*hik 2 java dao join

我是DAO世界的新手.我有3张桌子.

  • 消费者
  • 法案
  • 收据

消费者表包含以下字段

  • consumer_id
  • CONSUMER_NAME
  • 地址

比尔表包含

  • bill_number
  • bill_date
  • consumer_id
  • bill_amount

收据表包含

  • receipt_no
  • pay_date
  • 支付的金额
  • consumer_id

比尔表具有多对一与关系消费表, 回执表也有多对一与关系消费者表.

目前我已经创建了三个类

  1. 消费者
  2. 法案
  3. 收据

并为他们创建了DAO,如ConsumerDAO,BillDAO,ReceiptDAO.它们包含基本的CRUD操作.

现在我想要一个涉及这三个表中数据的消费者列表.我正在使用JOIN SQL查询.它如下:

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)";
Run Code Online (Sandbox Code Playgroud)

我想知道在哪个DAO我应该放置这个查询?我想补充一个比尔场收据场消费类,并添加一个getCurrentBillAndReceipt()的方法ConsumerDAO.这是正确的方法吗?

我读了以下问题:

如何为连接表创建DAO

还有一些,但我无法理解.

nma*_*rko 5

每次您需要访问每个消费者的最新Bill/Receipt时,不要尝试进行这样的查询,如果您的ConsumerPOJO变为以下内容:

public class Consumer {
    private Integer consumerId;
    private String consumerName;
    private String address;
    // join data into these variables
    private Set<Bill> bills = new Set<Bill>(0);
    private Set<Receipt> recipts = new Set<Receipt>(0);

    // add constructor and getter/setter methods here
}
Run Code Online (Sandbox Code Playgroud)

然后,在您ConsumerDAOfind方法中,您的方法可以具有以下逻辑:

public class ConsumerDAO {
    /*
     * Find a consumer without any transaction data JOINed in
     */
    public Consumer find(Integer consumerId){
        // create a new Consumer object
        // run SQL query to populate consumerId, consumerName, and address fields of new Consumer object
        // return new Consumer object
    }

    /*
     * Find a consumer with all transaction data JOINed in
     */
    public Consumer findWithTransactionData(Integer consumerId){
        // create new consumer object
        // run a query to get the the basic fields of the Consumer POJO, that also uses
        // JOINs to get all of the consumer's Bills and Receipts; store all of the latter
        // in the Consumer object's Sets
        // return consumer object
    }

    /*** Note that the following two methods could even go in the Consumer class at this point ***/
    public Bill getMostRecentBill(Consumer consumer){
        // iterate through the consumer's bills, and return the most recent one
    }

    public Receipt getMostRecentReceipt(Consumer consumer){
       // iterate through the consumer's receipts, and return the most recent one
    }
}
Run Code Online (Sandbox Code Playgroud)

这是你想要做的吗?我认为将"找到最近的"方法放在Consumer类本身中是最有意义的,因为那样你就不必将任何东西传递给方法作为参数; 如果你愿意的话getMostRecentBill(),你可以调用你得到的Consumer对象findWithTransactionData(Integer).然而,这通常被认为是一种糟糕的做法,因为POJO应该尽可能"愚蠢".因此,您希望如何处理这一点取决于您.