0

我最近一直在使用 Netbeans 和 MySQL 创建一个 Spring+Hibernate 应用程序,但我的代码块中遇到了一些问题:

public List listSystemProcess() {
        List procesosSistema = null;
        String hbQuery = "from TableProcess";

        try {
            Session hbSesion = HibernatePersistenceHandler.getSessionFactory().openSession();
            Transaction tx = hbSesion.beginTransaction();
            Query query = hbSesion.createQuery(hbQuery);
            procesosSistema = query.list();
            tx.commit();
            hbSesion.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return procesosSistema;
    }

这根本不起作用,我得到了一些与 SQL 语法异常有关的东西,告诉我“select id2_1,uj8_,5sd_11 from tb_2_1 ...”附近有一个语法错误

如果我创建一个 SQLQuery 而不是 HQL,它会完美运行,但我觉得这样做没有用,因为我应该为每个单独的进程都这样做,这很乏味,而且几乎与使用 JDBC 相同。

这有效:

String hbQuery = "select p.id,p.nombre,p.descripcion,p.frecuencia_uso,t.nombre "
        + "from tb_proceso_sistema p inner join tb_tipo_proceso t "
        + "on p.tb_tipo_proceso_id = t.id";
    Query query = hbSesion.createSQLQuery(hbQuery);

但是当我这样做时,我的 jsp 也发生了一些变化:

<c:forEach items="${procesosSistema}" var="proceso">
                <tr onmouseover="this.style.backgroundColor='#ffff66';"
            onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td><c:out value="${proceso[0]}"/></td>
                    <td><c:out value="${proceso[1]}"/></td>
                    <td><c:out value="${proceso[2]}"/></td>
                    <td><c:out value="${proceso[3]}"/></td>
                    <td><c:out value="${proceso[4]}"/></td>
                    <td></td>
                </tr>
            </c:forEach>

代替:

<c:forEach items="${procesosSistema}" var="proceso">
                <tr onmouseover="this.style.backgroundColor='#ffff66';"
            onmouseout="this.style.backgroundColor='#d4e3e5';">
                    <td><c:out value="${proceso.id}"/></td>
                    <td><c:out value="${proceso.nombre}"/></td>
                    <td><c:out value="${proceso.descripcion}"/></td>
                     ...
                    <td></td>
                </tr>
            </c:forEach>

当我在属性“show_sql”中设置时,它会显示一个奇怪的查询,如上所示,类似于:

"select id2_1,uj8_,5sd_11 from SGDP-mysql123.tb_2_1.... tb0"

如果有人可以帮助我,我将非常感激。

提前致谢。

4

3 回答 3

1

我有一个疑问,你提到

String hbQuery = "来自 TableProcess";

您的“TableProcess”类在 hbm.xml 文件中是否有与表的映射?

如果你在第二个查询中使用内连接,那么就不可能有这样的映射,你只能像这样使用它。

于 2013-07-10T15:51:55.827 回答
1

如果“TableProcess”类在 hbm.xml 中有映射并且数据库中存在表,请尝试以下代码:

public List listSystemProcess() {
    List<TableProcess> procesosSistema = null;
    String hbQuery = "SELECT p FROM TableProcess p";

    Session hbSesion = null;
    try {
        Session hbSesion =HibernatePersistenceHandler.getSessionFactory().openSession();
        Query query = hbSesion.createQuery(hbQuery);
        procesosSistema = query.list();

    } catch (Exception e) {
        e.printStackTrace(System.err);
    } finaly{
        hbSesion.close();
    }

    return procesosSistema;
}
于 2013-07-11T06:14:18.777 回答
0

Hi to everybody who answer to this question, but it was not a problem with the code but with the name of the tables and columns, since most of them contains the character "", when Hibernate generates a query, it uses "" as a character for its "encoding" or its "querying process". I mean, one of the columns was called process_id and frequence_number, but since that character is not admitted, I needed to replace it by processId and frequenceNumber (I don't know why, maybe because Hibernate considers it as a part of its encoding or something like that). What I needed to do, as I mentioned, is to fix the name of tables and columns to avoid using "_", after solving this, I needed to re-generate all my database mappings stuff and change the names for the new ones. Then it worked perfetly, hope this issue could help somebody with the same problem.

Thanks everybody for the answers, for sure I tried and maybe they would be useful in a similar scenario.

于 2013-07-11T21:24:01.947 回答