gpt4 book ai didi

mysql - Hibernate native sql表未找到错误

转载 作者:行者123 更新时间:2023-11-29 18:35:14 25 4
gpt4 key购买 nike

我正在尝试在 hibernate session 中运行 native 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)

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

最佳答案

(已编辑)

更好地使用表名称。

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

关于mysql - Hibernate native sql表未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45368889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com