简体   繁体   English

使用本机 SQL 创建表适用于 Hibernate/HSQLDB,插入失败。 为什么?

[英]Create table with native SQL works for Hibernate/HSQLDB, insert fails. Why?

I use HSQLDB and Hibernate.我使用 HSQLDB 和 Hibernate。 I have to implement some functionality on database level (trigger...) and because of this I have to execute some native SQL.我必须在数据库级别(触发器......)实现一些功能,因此我必须执行一些本机 SQL。 I create a new table (works) and then try to insert some data (fails).我创建了一个新表(有效),然后尝试插入一些数据(失败)。 I do this as SA such there should be no access right violation.我以 SA 身份执行此操作,因此不应违反访问权限。 Someone can guess why the user lacks privilege or object not found: A492Interface is thrown?有人可以猜到为什么user lacks privilege or object not found: A492Interface is throw?

This is my code (simplified):这是我的代码(简化):

session.createSQLQuery("CREATE TABLE entity_table_map (entity_name VARCHAR(50) NOT NULL PRIMARY KEY,table_name VARCHAR(50) NOT NULL)").executeUpdate();
session.createSQLQuery("INSERT INTO entity_table_map (entity_name,table_name) VALUES (\"A429Interface\",\"interface\")").executeUpdate();

Here is the relevant log:以下是相关日志:

[Server@19616c7]: 0:SQLCLI:SQLPREPARE CREATE TABLE entity_table_map (entity_name VARCHAR(50) NOT NULL PRIMARY KEY,table_name VARCHAR(50) NOT NULL)
[Server@19616c7]: 0:SQLCLI:SQLEXECUTE:5
[Server@19616c7]: 0:SQLCLI:SQLFREESTMT:5
[Server@19616c7]: 0:HSQLCLI:GETSESSIONATTR
[Server@19616c7]: 0:SQLCLI:SQLPREPARE INSERT INTO entity_table_map (entity_name,table_name) VALUES ("A492Interface","interface")
[Server@19616c7]: [Thread[HSQLDB Connection @13adc56,5,HSQLDB Connections @19616c7]]: 0:disconnected SA

And here the exception:这里有一个例外:

Exception in thread "main" 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 version.api.VersionAPI.setupDatabase(VersionAPI.java:38)
    at test.ui.VersionUI.setupDatabase(VersionUI.java:107)
    at test.SetupDatabase.main(SetupDatabase.java:9)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: A492Interface
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
    at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:202)
    ... 5 more

Update: It seems I can't execute any insert statement.更新:似乎我无法执行任何插入语句。 If I try to insert data into an other table this fails, too.如果我尝试将数据插入另一个表,这也会失败。 I must be doing something realy dumb... but didn't see it...我一定是在做一些非常愚蠢的事情……但没看到……

Details of the error mentioned by OP. OP提到的错误的详细信息。

session.createSQLQuery("INSERT INTO entity_table_map (entity_name,table_name) 
VALUES (\"A429Interface\",\"interface\")").executeUpdate();

In the SQL statement above, the two inserted values are strings, therefore the single quote (SQL quote character for strings) must be used.在上面的 SQL 语句中,插入的两个值都是字符串,因此必须使用单引号(字符串的 SQL 引号字符)。

When the double quote (SQL quote character for variable, column and table names) is used, the engine expects to find variables named "A429Interface", "interface" to insert into the table.当使用双引号(变量、列和表名的 SQL 引号字符)时,引擎希望找到名为“A429Interface”、“interface”的变量以插入到表中。 As no such variables exist, it reports user lacks privilege or object not found: A492Interface由于不存在此类变量,因此报告user lacks privilege or object not found: A492Interface

The error was to use double quotes for the strings instead of single quotes.错误是对字符串使用双引号而不是单引号。

Beyond the single-quote vs double-quote, if you are passing values in from a fron-end, you should really use bind variables so that users can't pass in values that would mess up concatenated dynamic SQL.除了单引号和双引号之外,如果您从前端传递值,您应该真正使用绑定变量,以便用户无法传递会混淆串联动态 SQL 的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM