views:

71

answers:

2

Hi i am trying to execute this simple HQL query to get a list of files from my database using:

private static final String SQL_GET_FILE_LIST = "select filename, size, id, type from fileobject";

@Override
public List<FileObject> getFileList(String type) {
    // TODO Auto-generated method stub
    List<FileObject> files = hbTemplate.find(SQL_GET_FILE_LIST);
    hbTemplate.flush();
    return files;
}

but i keep getting this exception:

  org.hibernate.hql.ast.QuerySyntaxException: fileobject is not mapped [select filename, size, id, type from fileobject]
    org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
    org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
    org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
    org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:257)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
    org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
    org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
    org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
    org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
    org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
    org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
    org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:923)
    org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
    org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
    org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)
    com.kc.models.DbFileHelper.getFileList(DbFileHelper.java:65)
    com.kc.models.FileHelper.getFileList(FileHelper.java:108)
    com.kc.Controllers.AppListController.getFileList(AppListController.java:63)
    com.kc.Controllers.AppListController.handleRequest(AppListController.java:34)
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

my hbm.xml file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

<hibernate-mapping package="com.kc.models.FileObject" >

    <class name="com.kc.models.FileObject" table="fileobject">
        <id name="id" column="ID">
            <generator class="native" />
        </id>
        <property name="filename" type="string" column="FILENAME" />
        <property name="type" type="string" column="TYPE" />
        <property name="size" type="double" column="SIZE" />
        <property name="file" type="blob" length="1000000000" column="FILE" />
    </class> 

</hibernate-mapping> 
+3  A: 

You're specifying the table name, not the class name; they differ in case. Try this:

private static final String SQL_GET_FILE_LIST = 
     "select filename, size, id, type from FileObject";

Admittedly it's been a while since I've done any Hibernate, but I suspect that's the problem...

Jon Skeet
you suspect correctly
Bozho
+5  A: 

Well, first of all, as the trace says, fileobject is not mapped but FileObject is. So change your HQL query into:

select filename, size, id, type from FileObject

Second, since you're using projections, you won't get a List<FileObject> but a List<Object[]>. So your code should be:

private static final String SQL_GET_FILE_LIST = "select filename, size, id, type from FileObject";

@Override
public List<Object[]> getFileList(String type) {
    // TODO Auto-generated method stub
    List<Object[]> files = hbTemplate.find(SQL_GET_FILE_LIST);
    hbTemplate.flush();
    return files;
}

Or, if you want to get a type safe result, you could use a constructor expression (assuming your class is providing the appropriate constructor):

select new FileObject(filename, size, id, type) from FileObject

And then you could use your current code.

private static final String SQL_GET_FILE_LIST = "select new FileObject(filename, size, id, type) from FileObject";

@Override
public List<FileObject> getFileList(String type) {
    // TODO Auto-generated method stub
    List<FileObject> files = hbTemplate.find(SQL_GET_FILE_LIST);
    hbTemplate.flush();
    return files;
}

Reference

Pascal Thivent
working now with no errors thanks again
jonney
@jonney You're welcome, glad it's solved.
Pascal Thivent