Ishan Tiwary
Ishan Tiwary

Reputation: 1008

Hibernate - Adding properties of another class in Criteria Restrictions

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.

Upvotes: 0

Views: 776

Answers (2)

Ady Junior
Ady Junior

Reputation: 1080

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();

Upvotes: 0

Sangram Badi
Sangram Badi

Reputation: 4274

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()

Upvotes: 1

Related Questions