Hibernate获取列表

时间:2014-12-29 05:16:07

标签: java hibernate

我在项目中使用Spring with Hibernate。我在一个返回列表的表中使用findAllById获取一个列表。之后我尝试从id和otherfield的同一个表中获取另一个列表。现在我得到一个过滤的列表,但不是我想象的那样..

第一个列表使用class.getById

包含如下内容
    id 1 val 5
    id 1 val 6
    id 1 val 2
    id 1 val 5
    id 1 val 6
    id 1 val 1

现在我只需要使用class.getByIdAndVal(1,5)来获得以下结果

    id 1 val 5
    id 1 val 5

但我得到以下结果

    id 1 val 5
    id 1 val 6

这里有什么不对,有人可以在这里提供帮助......

MyClass是

        List<LoanRepaymentSchedule> repay = loanService
                .getLoanRepaymentScheduleByLoanId(groupLoan.getLoanId());

        List<LoanRepaymentSchedule> rep1 = loanService.getLoanRepaymentScheduleByIdAndMember(loanAc.getId(), 5); 

当我尝试按照以下顺序

时,我可以得到完美的结果
        List<LoanRepaymentSchedule> rep1 = loanService.getLoanRepaymentScheduleByIdAndMember(loanAc.getId(), 5); 

        List<LoanRepaymentSchedule> repay = loanService
                .getLoanRepaymentScheduleByLoanId(groupLoan.getLoanId());

但我需要第一个人。 我的桌子在这里使用.....

    CREATE TABLE entity_loan_repayment_schedule
    (
      loan_id bigint NOT NULL,
      member_amount numeric(10,2) NOT NULL,
      member_count integer NOT NULL,
    )

这是我的DAO类方法。

    public List<LoanRepaymentSchedule> getLoanRepaymentScheduleById(long loanId) {
        Criteria c = commonRepository
                .createCriteria(LoanRepaymentSchedule.class);
        c.add(Restrictions.eq("loanId", Long.valueOf(loanId)));
        c.addOrder(Order.asc("year"));
        c.addOrder(Order.asc("month"));

        return c.list();
    }

    public List<LoanRepaymentSchedule> getLoanRepaymentHistoryByIdAndMember(long loanId, Integer memberCount) {
        Session session=sessionFactory.getCurrentSession();         
        SQLQuery queryy = session.createSQLQuery("SELECT * FROM entity_loan_repayment_schedule WHERE loan_id = :loanId AND member_count= :memberCount");
        queryy.setLong("loanId", loanId);
        queryy.setInteger("memberCount", memberCount);
        queryy.addEntity(LoanRepaymentSchedule.class);      
        return queryy.list();
    }

0 个答案:

没有答案