2017-06-23 6 views
0

これは私のVOである:私はcriteriaを使用した後NullPointerExceptionを取得していますJavaの春/休止状態:criteria.listを使用して上のNullpointException()

EmployeeVO.java

@Entity 
@Table(name="EMPLOYEES") 

public class EmployeeVO { 

@Id 

@Column(name="employee_id") 

@GeneratedValue(strategy=GenerationType.IDENTITY) 

private Integer employee_id; 

private String first_name; 

private String last_name; 

private String job_id; 

private String phone_number; 

private Integer salary; 

@Column(nullable=true, name="manager_id") 

private Integer manager_id; 

@Column(nullable=true, name="user_id") 

private Integer user_id; 

private Integer department_id; 

@OneToOne 

@JoinColumn(name="department_id", referencedColumnName="department_id", insertable=false, updatable=false) 

private DepartmentVO department; 

//getters and setters 

@Override 
public String toString() { 
    return "EmployeeVO [employee_id=" + employee_id + ", first_name=" + first_name + ", last_name=" + last_name 
      + ", job_id=" + job_id + ", phone_number=" + phone_number + ", salary=" + salary + ", manager_id=" 
      + manager_id + ", user_id=" + user_id + ", department_id=" + department_id + ", department=" 
      + department + "]"; 
} 

}

DAOImpl:

@Override 
public List<EmployeeVO> getEmployeeList(SearchForm searchForm) { 
    logger.info("SearchForm Detail :: "+searchForm); 
    Session session = this.sessionFactory.getCurrentSession(); 
    Criteria criteria = session.createCriteria(EmployeeVO.class); 
    criteria.setProjection(Projections.projectionList()); 
    if (null != searchForm.getOpt() || !"".equals(searchForm.getOpt())) { 
     criteria.add(Restrictions.like(searchForm.getOpt(), searchForm.getKeyword())); 
    } 
    if (null != searchForm.getMaxSalary()) { 
     criteria.add(Restrictions.lt("salary", searchForm.getMaxSalary()));   
    } 
    if (null != searchForm.getMinSalary()) { 
     criteria.add(Restrictions.gt("salary", searchForm.getMinSalary())); 
    } 
    if (null != searchForm.getOrderBy() || !"".equals(searchForm.getOrderBy())) { 
     if ("asc".equals(searchForm.getOrderBy())) { 
      criteria.addOrder(Order.asc(searchForm.getSort())); 
     } else if ("desc".equals(searchForm.getOrderBy())) { 
      criteria.addOrder(Order.desc(searchForm.getSort())); 
     } 
    } 
    criteria.setFirstResult((searchForm.getPageNo()-1)*searchForm.getDisplay()); 
    criteria.setMaxResults(searchForm.getDisplay()); 
    List<EmployeeVO> employeeList = criteria.list(); 

    logger.info("Employee List Size :: "+employeeList.size()); 
    return employeeList; 
} 

criteria.list()は、NullPointExceptionをスローなぜ誰もが知っていますか?

私はquery.list()を使用しても大丈夫でしたが、基準に変更した後にエラーが発生しました。

+0

https://stackoverflow.com/questions/15043866/criteria-list-shows-null-pointer-exceptionを参照してください。 – user7294900

答えて

0

クエリを条件に書き込む場合は、このようなクエリを作成します。

public List<EmployeeVO> getEmployeeList(SearchForm searchForm) { 
logger.info("SearchForm Detail :: "+searchForm); 
private String first_name; 
private String last_name; 
ProjectionList projectionList = Projections.projectionList(); 
projectionList .add(Projection.property("first_name"),"first_name"); 
projectionList .add(Projection.property("middle_name"),"middle_name"); 
projectionList .add(Projection.property("last_name"),"last_name"); 

Session session = this.sessionFactory.getCurrentSession(); 
Criteria criteria = session.createCriteria(EmployeeVO.class); 
criteria.setProjection(projectionList); 
if (null != searchForm.getOpt() || !"".equals(searchForm.getOpt())) { 
    criteria.add(Restrictions.like(searchForm.getOpt(), searchForm.getKeyword())); 
} 
if (null != searchForm.getMaxSalary()) { 
    criteria.add(Restrictions.lt("salary", searchForm.getMaxSalary()));   
} 
if (null != searchForm.getMinSalary()) { 
    criteria.add(Restrictions.gt("salary", searchForm.getMinSalary())); 
} 
if (null != searchForm.getOrderBy() || !"".equals(searchForm.getOrderBy())) { 
    if ("asc".equals(searchForm.getOrderBy())) { 
     criteria.addOrder(Order.asc(searchForm.getSort())); 
    } else if ("desc".equals(searchForm.getOrderBy())) { 
     criteria.addOrder(Order.desc(searchForm.getSort())); 
    } 
} 
criteria.setFirstResult((searchForm.getPageNo()-1)*searchForm.getDisplay()); 
criteria.setMaxResults(searchForm.getDisplay()); 
List<EmployeeVO> employeeList = criteria.list(); 

logger.info("Employee List Size :: "+employeeList.size()); 
return employeeList; } 
関連する問題