The method below opens the session, executes hql and returns a string list properly. I would like to return a "String" result instead of a List.
public List<String> getStates() throws Exception {
try
{
Session session = sessionFactory.openSession();
return session.createQuery("from States").list();
}
catch(Exception e)
{
//Logging
}
finally
{
if(session !=null && session.isOpen())
{
session.close();
session=null;
}
}
return null;
}
Here is what I have so far but noticed I can't use toString method here. How do I return a string instead of a list of strings?
public String getStates() throws Exception {
try
Session session = sessionFactory.openSession();
Query q = session.createQuery("from States");
List<String> list = (List<String>) q.list();
return list.toString();
After trying code from below I get a .domain.States cannot be cast to java.lang.String ClassCastException. So I changed my query. When I run this query (select a.stateName, from States a) I get statename but I also want stateCode. When I run this query ("select a.stateName, a.stateCode from States a") I get the ClassCastException again.
Do I have to iterate over the values?