如何优化Jooq中的SQL查询以返回表对象

时间:2013-12-13 12:03:28

标签: java sql jooq

我在下面的代码中使用Join

运行查询
@Transactional
public Result<Record> getFolderFeeList(int argFolderRSN, String argOrderBy) {

    Transaction transaction = Transaction.current();
    // List<ValidAccountFeeRow> validSiteOptionList = transaction.daos().getValidAccountFeeDao().findAll();

    SelectQuery<Record> selectQuery = transaction.selectQuery();
    selectQuery.addSelect(
            Routines.fFoldername(argFolderRSN).as("FolderName"),
            AccountBillFee.ACCOUNT_BILL_FEE.FOLDER_RSN,
            AccountBillFee.ACCOUNT_BILL_FEE.PARENT_ACCOUNT_BILL_FEE_RSN,
            AccountBillFee.ACCOUNT_BILL_FEE.FEE_CODE,
            AccountBillFee.ACCOUNT_BILL_FEE.PROCESS_RSN,
            AccountBillFee.ACCOUNT_BILL_FEE.SECURITY_CODE,
            AccountBillFee.ACCOUNT_BILL_FEE.FEE_AMOUNT,
            Routines.fGetpaidinfullflag(AccountBillFee.ACCOUNT_BILL_FEE.BILL_NUMBER, AccountBill.ACCOUNT_BILL.PAYMENT_OPTION,
                    AccountBillFee.ACCOUNT_BILL_FEE.FEE_LEFT.cast(Double.class), AccountBill.ACCOUNT_BILL.PAID_IN_FULL_FLAG.cast(String.class))
                    .as("PaidInFullFlag"), AccountBillFee.ACCOUNT_BILL_FEE.MANDATORY_FLAG, AccountBill.ACCOUNT_BILL.DUE_DATE,
            AccountBillFee.ACCOUNT_BILL_FEE.BILL_NUMBER, AccountBillFee.ACCOUNT_BILL_FEE.ACCOUNT_BILL_FEE_RSN,
            AccountBillFee.ACCOUNT_BILL_FEE.FEE_COMMENT);
    selectQuery.addFrom(AccountBillFee.ACCOUNT_BILL_FEE);
    selectQuery.addJoin(AccountBill.ACCOUNT_BILL, JoinType.LEFT_OUTER_JOIN,
            AccountBill.ACCOUNT_BILL.BILL_NUMBER.eq(AccountBillFee.ACCOUNT_BILL_FEE.BILL_NUMBER));
    selectQuery.addJoin(ValidAccountFee.VALID_ACCOUNT_FEE,
            AccountBillFee.ACCOUNT_BILL_FEE.FEE_CODE.eq(ValidAccountFee.VALID_ACCOUNT_FEE.FEE_CODE));
    selectQuery.addConditions(AccountBillFee.ACCOUNT_BILL_FEE.FOLDER_RSN.eq(argFolderRSN));
    if (argOrderBy == null) {
        selectQuery.addOrderBy(AccountBillFee.ACCOUNT_BILL_FEE.FOLDER_RSN.asc(), AccountBillFee.ACCOUNT_BILL_FEE.BILL_NUMBER.asc(),
                AccountBillFee.ACCOUNT_BILL_FEE.FEE_CODE.asc());
    } else {
        // To Do selectQuery.addOrderBy(argOrderBy);
    }
    Result<Record> result = selectQuery.fetch();
    return result;

}

现在这个查询会以某种方式返回AccountBillFee表整个对象以及函数值吗?正如我在JOOQ中看到的那样,如果我自己创建一个对象,它总是插入一条新记录而不是更新它。

2 个答案:

答案 0 :(得分:2)

您可以一次性将所有AccountBillFee列添加到SELECT语句中:

selectQuery.addSelect(AccountBillFee.fields());

由于您希望将获取的(丰富的)AccountBillFeeRecord记录再次存储到数据库中,我建议您尝试以下操作:

// This is the "weakly typed", generic record with the additional columns
Record record =
DSL.using(configuration)
   .select(...)
   .from(ACCOUNT_BILL_FEE)
   .fetchOne();

// This is one way to transform the above record into a
// "table-typed" record that you can store
AccountBillFeeRecord r1 = record.into(ACCOUNT_BILL_FEE);
r1.update();

答案 1 :(得分:0)

感谢 @Lukas ,这是我正在寻找的解决方案

     @Transactional
    public Result<Record> getFolderFeeList(int argFolderRSN, String argOrderBy) {

   Transaction transaction = Transaction.current();
 // List<ValidAccountFeeRow> validSiteOptionList     transaction.daos().getValidAccountFeeDao().findAll();

  SelectQuery<Record> selectQuery = transaction.selectQuery();
  selectQuery.addSelect(Routines.fFoldername(argFolderRSN).as("FolderName"),
                    Routines.fGetpaidinfullflag(AccountBillFee.ACCOUNT_BILL_FEE.BILL_NUMBER, AccountBill.ACCOUNT_BILL.PAYMENT_OPTION,
                            AccountBillFee.ACCOUNT_BILL_FEE.FEE_LEFT.cast(Double.class), AccountBill.ACCOUNT_BILL.PAID_IN_FULL_FLAG.cast(String.class))
                            .as("PaidInFullFlag"),AccountBill.ACCOUNT_BILL.DUE_DATE);
 selectQuery.addSelect(AccountBillFee.ACCOUNT_BILL_FEE.fields());
selectQuery.addFrom(AccountBillFee.ACCOUNT_BILL_FEE);
selectQuery.addJoin(AccountBill.ACCOUNT_BILL, JoinType.LEFT_OUTER_JOIN,
                    AccountBill.ACCOUNT_BILL.BILL_NUMBER.eq(AccountBillFee.ACCOUNT_BILL_FEE.BILL_NUMBER));
 selectQuery.addJoin(ValidAccountFee.VALID_ACCOUNT_FEE,
   AccountBillFee.ACCOUNT_BILL_FEE.FEE_CODE.eq(ValidAccountFee.VALID_ACCOUNT_FEE.FEE_CODE));
            selectQuery.addConditions(AccountBillFee.ACCOUNT_BILL_FEE.FOLDER_RSN.eq(argFolderRSN));
            if (argOrderBy == null) {
                selectQuery.addOrderBy(AccountBillFee.ACCOUNT_BILL_FEE.FOLDER_RSN.asc(), AccountBillFee.ACCOUNT_BILL_FEE.BILL_NUMBER.asc(),
                        AccountBillFee.ACCOUNT_BILL_FEE.FEE_CODE.asc());
            } else {
                // To Do selectQuery.addOrderBy(argOrderBy);
            }
            Result<Record> result = selectQuery.fetch();
            for(Record record : result){
            AccountBillFeeRecord r1 = record.into(AccountBillFee.ACCOUNT_BILL_FEE);
            System.out.println(record.getValue("FolderName"));
            }
            return result;

        }