简体   繁体   中英

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

I use HSQLDB and Hibernate. I have to implement some functionality on database level (trigger...) and because of this I have to execute some native 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. Someone can guess why the user lacks privilege or object not found: A492Interface is thrown?

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.

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.

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. As no such variables exist, it reports 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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