Session/EntityManager is closed on calling list() function of Criteria api

Viewed 29

I am using Criteria apis to fetch records from database. When I am using criteria.list(), it throws java.lang.IllegalStateException: Session/EntityManager is closed. Where as other places where I have used criteria apis this is not the case and they are working fine. Here is how I an using the api,

  @Autowired
  @PersistenceContext(unitName = "logsEntityManagerFactory")
  private EntityManager entityManager;
  
  public Criteria createCriteria() {
    Session session = entityManager.unwrap(Session.class);
    return session.createCriteria(Script.class);
  }

  public List<Object> getPageContent(int from, int pageSize){
    if(from < 0) return new ArrayList<>();
    pageSize = Math.min(25, Math.max(10, pageSize));
    Criteria criteria = createCriteria();
    criteria.add(Restrictions.eq(Constants.TYPE,  Constants.SCRIPT_TYPES.globalScript));
        criteria.setFirstResult(from);
    criteria.setMaxResults(pageSize);
    return criteria.list(); // session/entity manager closed error.
}

I am using same method to use criteria api in other places in my code. Can anybody explain me what is the issue with this code.

0 Answers
Related