I am trying to learn hibernate. Right now I am studying criteria query. Here I have got trouble with Restrictions.isEmpty(). Can anyone please help me on this please ? Here are my attempts below ::
In my method >>>
Criteria cr = session.createCriteria(EmployeeCriteria.class);
cr.add(Restrictions.isEmpty("salary"));
List results = cr.list();
for (Iterator iterator = results.iterator(); iterator.hasNext();) {
EmployeeCriteria employee = (EmployeeCriteria) iterator.next();
System.out.println("##### - Employee #" + employee.getId() + " - #####");
System.out.println("===============================");
System.out.print("Employee ID: " + employee.getId());
System.out.print(" || First Name: " + employee.getFirstName());
System.out.print(" || Last Name: " + employee.getLastName());
System.out.println(" || Salary: " + employee.getSalary());
}
And it's giving this error >>>
org.hibernate.MappingException: Property path [criteria.EmployeeCriteria.salary] does not reference a collection
at org.hibernate.criterion.AbstractEmptinessExpression.getQueryableCollection(AbstractEmptinessExpression.java:84)
at org.hibernate.criterion.AbstractEmptinessExpression.toSqlString(AbstractEmptinessExpression.java:63)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
MY EmployeeCriteria POjo
import javax.persistence.*;
@Entity
@Table(name = "employee_criteria")
public class EmployeeCriteria {
@Id @GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "salary")
private int salary;
public EmployeeCriteria() {}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName( String first_name ) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName( String last_name ) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}
isEmpty()
is a method for lists. You are checking salary, which probably isn't a list, but more like a float or something. Use something like criteria.add(Restrictions.ne("salary",""));
or criteria.add(Restrictions.isNotNull("salary"));
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With