Problem Description:
The previous article records the issues derived from saveAll()'s batch execution of custom sql:
saveAll()'s batch execution of custom sql
Use entityManager to execute batch new statements, and an error will be reported when executing the sql statement. The error message is as follows:
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178)
at org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:204)
at org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1575)
at org.hibernate.query.internal.NativeQueryImpl.doExecuteUpdate(NativeQueryImpl.java:274)
at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1504)
... 109 common frames omitted
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 '疫':少儿防护科普','0','1','2022-01-06T15:33','2022-01-06T16:00','111' at line 1
There are single quotes in the name (Preventing 'epidemic': Popular science on children's protection)
Cause Analysis:
A certain field contains special symbols such as single quotes, causing an error when executing the SQL statement.
solution:
Process special symbol values in fields, single quotes’Replace with two single quotes‘’
public static String parseString(String name){
if(name.indexOf("'") > 0 ){
name = name.replaceAll("'", "''");
}
return name;
}
Perform character replacement processing on fields that may have special fields, perfect solution! ! !