Hibernate setParameter将单引号添加到字符串

时间:2016-08-18 09:37:24

标签: java spring hibernate error-handling prepared-statement

我正在尝试使用hibernate prepared语句创建新表。看起来"setparameter("values", value)"会为查询添加额外的引号。 我的代码:

String hql = "create table :values " + "(name VARCHAR(50))";
        Query my = session.createSQLQuery(hql);
        my.setParameter("values", value);
        my.executeUpdate();
        session.close();

错误:

SEVERE: Servlet.service() for servlet [contextServlet] in context with path [/SpringSafeHouseService2.0] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause
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 ''merkurijuss' (name VARCHAR(50))' at line 1

1 个答案:

答案 0 :(得分:0)

您不能在准备好的Statement中使用表名作为参数。你必须把它放进刺痛中:

    String hql = "create table "+ value+ " (name VARCHAR(50))";
    Query my = session.createSQLQuery(hql);
    my.executeUpdate();
    session.close();