I have two tables
Loan (id, amount, duration)
LoanStatus(id, status, loan_id) // just an example, but it has lot more fields in this table
Loan.java
public class Loan{
private int id;
private int amount;
private int duration;
private LoanStatus loanStatus;
// getter and setters
}
LoanStatus.java
public class LoanStatus{ // just an example, but it has many fields than what actually given
private int id;
private String status;
private Loan loan;
//getter and setters
}
Now I would like to get only amount
, duration
, loanStatus.status
using Projections. I've used createAlias()
to successfully fetch that particular column, but the problem occurs when setting it to a setter.
Criteria criteria = getSession().createCriteria(Loan.class,"loan");
criteria.createAlias("loan.loanStatus", "loanStatus")
.setProjection(Projections.projectionList().add(Projections.property("id"),"id")
.add(Projections.property("amount"),"amount")
.add(Projections.property("duration"),"duration")
.add(Projections.property("loanStatus.status"), "loanStatus"))
.add(Restrictions.eq("id", id))
.setResultTransformer(Transformers.aliasToBean(Loan.class));
return criteria.list();
I've an error which as follows.
IllegalArgumentException occurred while calling setter for property [com.site.dto.Loan.loanStatus (expected type = com.site.dto.LoanStatus)]; target = [com.site.dto.Loan@4083974a], property value = [Pending]
So I'm getting my expected column value "Pending", but the problem is when setting it to a setter. I've seen many question for projections, but most of them was based on Restrictions using Projections but not fetching a child's particular column using projections.