I'm using Spring for my application framework. I've read several posts about an
org.hibernate.QueryException: The collection was unreferenced error.
To simplify the calls I created a controller method bellow, which calls Dao layer directly:
Code:
@RequestMapping(method=RequestMethod.GET )
@Transactional
public @ResponseBody Map<String,? extends Object> getAll(
@RequestParam(required=false) String filter,
@RequestParam(required=false) String start,
@RequestParam(required=false) String limit,
@RequestParam(required=false) String sort,
@RequestParam(required=false) String dir
) {
try{
Collection<?> results = this.taskDao.getCollection();
return MessageProcessor.createRecordListMessage(null);
} catch (Exception e) {
logger.error(e);
return MessageProcessor.createError(e.getLocalizedMessage());
}
}
Here is my Dao:
Code:
package com.objectverse.projects.task.repository;
import java.util.Collection;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.objectverse.projects.task.model.Task;
@Repository("taskDao")
public class TaskDAO {
@Autowired
private SessionFactory sessionFactory;
public TaskDAO() {
super();
}
@SuppressWarnings("unchecked")
//public <T> Collection<T> getCollection() {
public Collection<Task> getCollection() {
Session session = this.sessionFactory.getCurrentSession();
long size = ((Long)session.createQuery("select count(*) from " + Task.class.getName() + " o ")
.iterate()
.next())
.longValue();
Query query = session.createQuery("select o from " + Task.class.getName() + " o ");
Collection<Task> records = query.list();
Collection<Task> filteredCollection = this.filterCollection(records, session);
return filteredCollection;
}
private <T> Collection<T> filterCollection(Collection<T> collection, Session s) {
Query filterQuery = s.createFilter(collection, "where this.title like 'I%'");
return filterQuery.list();
}
}
The error is raised in the
Collection<Task> filteredCollection = this.filterCollection(records, session); line. Why?
Any help appreciated.
Bojan