我有这个方法.private final void updateAllTableFields(final Class clazz){final String tableName = ((Table)clazz.getAnnotation(Table.class)).name();final String sqlQuery = new StringBuilder(SET @ids = ...
我有这个方法.
private final void updateAllTableFields(final Class clazz){
final String tableName = ((Table)clazz.getAnnotation(Table.class)).name();
final String sqlQuery = new StringBuilder("SET @ids = NULL; ")
.append("UPDATE ")
.append(tableName)
.append(' ')
.append("set activeRecord=:activeRecord ")
.append("where activeRecord=true and updateable=true ")
.append("and (SELECT @ids \\:= CONCAT_WS(',', id, @ids)); ")
.append("select @ids;")
.toString();
final Query query = session.createSQLQuery(sqlQuery)
.setParameter("activeRecord",Boolean.FALSE);
final Object idsList=query.uniqueResult();
System.out.println("idsList = " + idsList);
}
我想做一个更新,并返回受影响的ID这个工作完美使用rawSQL以字符串方式返回id但我不能使用Hibernate任何提示工作!
在此先感谢您的问候.
UPDATE
我需要做一个更新并返回受影响的ID !!我不想做一个简单的更新.
你可以check it out the original question here pal:
https://stackoverflow.com/questions/44604763/java-hibernate-tips-about-update-all-table-fields-performance
UPDATE
错误是
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
at org.hibernate.loader.Loader.doQuery(Loader.java:909)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:966)
at company.nuevemil.code.finalizarEntornoDePrueba(Test.java:56)
at company.nuevemil.code.main(Test.java:27)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE student set activeRecord=false,uid=1 where activeRecord=true at line 1
解决方法:
我想,你将无法以Hibernate的方式制作它.
Hibernate独立于数据库.但初始化变量的查询部分(我的意思是设置@ids = null;)不能在所有关系数据库中移植,所以我不希望它在某个地方的Hibernate API中.
本文标题为:Java Hibernate org.hibernate.exception.SQLGrammarException:无法在createSQLQuery上提取ResultSet
基础教程推荐
- Spring Boot 使用 SSE 方式向前端推送数据详解 2023-04-07
- kafka添加安全验证配置方式 2023-06-24
- SpringMVC响应处理详细解读 2023-03-21
- 详解Spring中@Valid和@Validated注解用法 2023-03-15
- java IP归属地功能实现详解 2023-02-28
- Java实现List去重的方法详解 2022-11-08
- Java实现窗体程序显示日历表 2023-01-02
- 基于Springboot一个注解搞定数据字典的实践方案 2022-12-27
- jsp页面中引用其他页面的简单方法 2023-08-01
- Java中的null用法介绍 2023-10-08