Hibernate SQL混合了来自不同表的两个列值

时间:2018-04-25 10:13:02

标签: java sql hibernate

我正在运行此查询

        Query q = session.getCurrentSession()
            .createSQLQuery("SELECT alt.*, cpp.*, l.*, u.*, br.*,np.*, afp.*, lmp.*  FROM ActiveLoansTable alt " +
                    "JOIN loan l ON l.loanId=alt.loanId " +
                    "JOIN custpaymentplan cpp ON cpp.loanId=alt.loanId " +
                    "JOIN branches br ON br.branchId=l.branch_id " +
                    "JOIN users u ON u.userId=l.userId " +
                    "JOIN naturalPerson np ON np.personId=l.customerId " +
                    "LEFT JOIN additionalfinancialparameters afp on afp.loanId = l.loanId " +
                    "LEFT JOIN loanmanagementprice lmp on lmp.loanId = l.loanId " +
                    "WHERE l.status = :statusActive and (alt.isLateInSchedule=1 or alt.isLateOutSchedule=1) ")
            .addEntity("alt", ActiveLoanTable.class)
            .addEntity("cpp", CustPaymentPlan.class)
            .addEntity("l", Loan.class)
            .addEntity("u", User.class)
            .addEntity("br", Branch.class)
            .addEntity("np", NaturalPerson.class)
            .addEntity("afp", AdditionalFinancialParameters.class)
            .addEntity("lmp", LoanManagementPrice.class);

然后我将结果投射到对象:

        for (Object[] row : rows) {

        ActiveLoanTable alt = (ActiveLoanTable) row[0];
        CustPaymentPlan cpp = (CustPaymentPlan) row[1];
        Loan l = (Loan) row[2];
        User u = (User) row[3];
        Branch br = (Branch) row[4];
        NaturalPerson np = (NaturalPerson) row[5];
        AdditionalFinancialParameters afp = (AdditionalFinancialParameters) row[6];
        LoanManagementPrice lmp = (LoanManagementPrice) row[7];}

其中naturalPerson和用户都有列firstnamelastname。我想返回它们,但最终结果naturalperson值被用户值覆盖。因此np.firstname变为u.firstname

0 个答案:

没有答案