0

I have a table Loan application whose pojo is

class LoanApplication{
int id;
int loanNo;
...
}

I have another pojo

class LoanFlow{
int id;
int loanId;
date reviewDate;
...
}

Here loanId of loanFlow is the foreign key mapped to the id of LoanApplication.

I have to fetch all the loan applications with the reviewDate.

I am trying to write a criteria like:

Criteria criteria = getSession().createCriteria(LoanApplication.class);
criteria.add(Restrictions.eq("id", someId));

How can I fetch the reviewDate also from LoanFlow with this criteria.

Ady Junior
  • 1,040
  • 2
  • 10
  • 18
Ishan Tiwary
  • 938
  • 6
  • 15
  • 37

2 Answers2

1
Criteria criteria = getSession().createCriteria(LoanApplication.class, "loApp");
criteria.createAlias("loApp.loanFlow", "flow");
criteria.add(Restrictions.eq("flow.id", 1));

You can directlly use HQL also. using createQuery()

Sangram Badi
  • 4,054
  • 9
  • 45
  • 78
0

You can do it with subqueries, if there isn't ManyToOne or OneToMany annotations to the relationship between entities:

DetachedCriteria subQuery = DetachedCriteria.forClass(LoanFlow.class, "loanFlow");

/*Here there is a between, but you can change it to your necessity*/
subQuery.add(Restrictions.between("loanFlow.reviewDate", dateBegin, dateEnd));
subQuery.setProjection(Projections.property("loanFlow.loanId"));

Criteria criteria = getSession().createCriteria(LoanApplication.class, "loanApplication");        
Subqueries.propertyIn("loanApplication.id", subQuery);

List<LoanApplication> list = criteria.list();
Ady Junior
  • 1,040
  • 2
  • 10
  • 18