package main.java.dao;

import java.util.List;

import org.hibernate.SQLQuery;

import main.java.domain.Answer;

public class AnswerDao extends GenericDaoImpl<Answer> {
	@SuppressWarnings("unchecked")
	public List<Object[]> getCSV(List<Integer> questionIds) {
		getSession().beginTransaction();
		String SQL = "select * from (select userid,questionid,answer from ntr_answer) PIVOT(max(answer) for questionid in(";
		for(int i : questionIds) {
			SQL += i+",";
		}
		SQL = SQL.substring(0, SQL.length()-1)+"))";
		SQLQuery SQLquery = getSession().createSQLQuery(SQL);
		List<Object[]> rows = SQLquery.list();
		return rows;
	}
}

package main.java.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;

import main.java.domain.Answer;

public class AnswerDao extends GenericDaoImpl<Answer> {
	public List<Answer> getAnswersByQuestionId(int questionId) {
		getSession().beginTransaction();
		Query query = getSession().createQuery("from Question where survey.id =:id").setParameter("id", questionId);
		return query.list();
	}
	@SuppressWarnings("unchecked")
	public List<Object[]> getCSV(List<Integer> questionIds) {
		getSession().beginTransaction();
		String SQL = "select * from (select userid,questionid,answer from ntr_answer) PIVOT(max(answer) for questionid in(";
		for(int i : questionIds) {
			SQL += i+",";
		}
		SQL = SQL.substring(0, SQL.length()-1)+"))";
		SQLQuery SQLquery = getSession().createSQLQuery(SQL);
		List<Object[]> rows = SQLquery.list();
		return rows;
	}
}

fix getAnswersByQuestionId