【问题标题】:Hibernate native sql table not found errorHibernate native sql table not found错误
【发布时间】:2017-07-29 17:15:47
【问题描述】:

我正在尝试在休眠会话中运行本机 SQL 删除,但我收到一个异常,抱怨表别名。如果我在 SQL 客户端中运行 sql 查询,那么它工作正常。

String sql = 'delete c from child c join parent p on c.parent_id=p.id where p.some_id = :someId'
SQLQuery deleteQuery = sessionFactory.currentSession.createSQLQuery(sql)
deleteQuery.setParameter( 'someId', some.id.longValue() )
deleteQuery.executeUpdate()

在我的单元测试中抛出异常:

[main] ERROR util.JDBCExceptionReporter  - Table "C" not found; SQL statement:

delete  c  from child  c  join parent p on c.parent_id=p.id where p.some_id = ? [42102-164]

org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:219)
at org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1310)
at org.hibernate.impl.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:396)
at org.hibernate.Query$executeUpdate.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)

关于为什么这不能通过休眠工作的任何建议?

【问题讨论】:

    标签: mysql sql hibernate


    【解决方案1】:

    (已编辑)

    更好地使用表名。

    String sql = 'DELETE child FROM child INNER JOIN parent ON child.parent_id = parent.id WHERE parent.some_id = :someId';
    

    添加

    这是一个适合你的示例代码(我的):

    ...
    
    public boolean eliminar( Child some_id )
    {
        boolean result  = false;
        Session session = null;
        Transaction rs  = null;
    
        try
        {
            session = sessionFactory.openSession();
            rs = session.beginTransaction();
            rs.setTimeout(5);
    
            String query_string = "DELETE child FROM child INNER JOIN parent ON child.parent_id = parent.id WHERE parent.some_id = :someId";
            query_string.setParameter( 'someId', some_id );
            Query q = session.createQuery(query_string);
            q.executeUpdate();
    
            rs.commit();
    
            result = true;
        }
        catch(RuntimeException e)
        {
            try
            {
                rs.rollback();
            }
            catch(RuntimeException rbe)
            {
                System.out.println(rbe.getMessage());
            }
            System.out.println(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return result;
    }
    ...
    

    【讨论】:

    • 错误:ERROR util.JDBCExceptionReporter - SQL 语句中的语法错误“从子 c 加入 [*] 父 p on c.parent_id=p.id where p.some_id = ?”; SQL 语句:从子 c 删除加入父 p on c.parent_id=p.id where p.some_id = ? [42000-164]
    • 我编辑了我的条目,用新的查询再试一次。我替换表名的别名。错误是mysql没有匹配到表。
    • ERROR util.JDBCExceptionReporter - SQL 语句中的语法错误 "delete child from[*] child inner join parent p on child.some_id=p.id where p.some_id = ?"; SQL 语句:
    • 嗯,这是另一个消息。我找到了这个stackoverflow.com/questions/8403388/…
    • 如前所述,SQL 语句是正确的,因为它在 SQL 客户端中工作。出于某种原因,hibernate 似乎确实喜欢它。我是否得到正确的代码来运行本机 SQL 语句?我可能缺少任何可能的休眠属性?
    猜你喜欢
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多