2

您好我正在尝试执行这个简单的 HQL 查询以使用以下方法从我的数据库中获取文件列表:

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;
}

但我不断收到此异常:

  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)

我的 hbm.xml 文件

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

<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> 
4

2 回答 2

7

好吧,首先,正如痕迹所说,fileobject is not mapped但是FileObject。因此,将您的 HQL 查询更改为:

select filename, size, id, type from FileObject

其次,由于您使用的是投影,因此您不会得到 aList<FileObject>而是 a List<Object[]>。所以你的代码应该是:

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;
}

或者,如果您想获得类型安全的结果,您可以使用构造函数表达式(假设您的类提供了适当的构造函数):

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

然后你可以使用你当前的代码。

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;
}

参考

于 2010-09-14T13:00:01.277 回答
3

您指定的是表名,而不是类名;它们的大小写不同。试试这个:

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

诚然,我已经有一段时间没有做过任何休眠了,但我怀疑这就是问题所在......

于 2010-09-14T12:56:05.853 回答