I am testing create method for my application .
In test I have embedded hsql db , and using Spring jdbcTemplate
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:/sql/db/db-schema.sql"/>
<jdbc:script location="classpath:/sql/db/db-data.sql"/>
</jdbc:embedded-database>
where db-data.sql is
INSERT INTO C_ContactType(name) VALUES ('bob_eng991');
INSERT INTO C_ContactType(name) VALUES ('bob_eng2');
INSERT INTO C_ContactType(name) VALUES ('bob_eng3');
INSERT INTO C_ContactType(name) VALUES ('bob_eng422');
db-schema.sql is
CREATE TABLE C_ContactType (
contactTypeId INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR(90) NOT NULL,
deletedOn DATETIME,
deletedBy INT
);
and my test is
#Test
void test_createClassifier() {
mockMvc.perform(MockMvcRequestBuilders.post("/classifiers/{category}", "ContactType")
.contentType(MediaType.APPLICATION_JSON)
.param("languageId", String.valueOf(3))
.content("{ \"name\": \"bob_eng100\"}".getBytes()))
.andExpect(status().isOk())
}
running test I get errors here
ResultSet rs = jdbcTemplate.insertWithKey(tablePath,
insert -> insert.columns(paths.toArray(new Path[paths.size()]))
.values(params.toArray(new Object[paths.size()]))
.executeWithKeys());
where tablePath indicates to "C_ContactType" ,
paths is an ArrayList with my table columns except autoIncrement field ,
and params is an ArrayList with values (except autoIncrement field)
And logs
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: contactTypeId
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.Table.getColumnIndex(Unknown Source)
at org.hsqldb.Table.getColumnIndexes(Unknown Source)
at org.hsqldb.StatementDML.setGeneratedColumnInfo(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 82 more
The error show the name of the column requested by the framework. The name is in mixed case contactTypeId.
Your table declaration does not have double-quote characters around the table and column names, therefore the names are converted to upper-case by the database engine. You need to change the table definition:
CREATE TABLE C_ContactType (
"contactTypeId" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
Related
I use hibernate to insert data in a table using autogenerated GUID, but insertion fails sometimes with duplicate GUID exception.
For Example:
From Logs , insertion fails for the first 2 attempts by printing the duplicate GUID '0500edac-0074-4324-3436-31444231342d'. The time taken are as follows
1st attempt :08-27-2018 04:27:00.012,
2nd attempt :08-27-2018 04:27:01.024,
3rd attempt was not logged ,as it was successful
but in the database I see a row with GUID '0500edac-0074-4324-3436-31444231342d' created at '08-27-2018 04:27:01.054'
So I am not sure why I am getting the exceptions for the first 2 attempts and then successfully it inserts the 3rd time.
SQL Table Properties: I have a SQL Server table named "DataHistory" with a column named
"DataHistoryGuid" with the following properties uniqueidentifier,ROWGUIDCOL,Primary Key column,newsequentialid .
Hibernate Properties:
I am using hibernate to store the data in that table, for the GUID column, I am using the
<id name="dataHistoryGuid" type="java.util.UUID" >
<column name="DataHistoryGuid"/>
<generator class="guid"/>
</id>
The following is the exception trace:
[event.def.AbstractFlushingEventListener:performExecutions:324]
Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not
insert: [com.testProj.dataprocessor.model.sql.SqlDataHistory]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2295)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2688)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:748)
at com.testProj.dataprocessor.common.sql.hibernate.HibernateSession.upsertDataHistory(HibernateSession.java:505)
at com.testProj.dataprocessor.common.sql.SqlStore.upsertDataHistory(SqlStore.java:92)
at com.testProj.dataprocessor.common.sql.SqlStore$$FastClassByCGLIB$$18d897d8.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:77)
at com.testProj.dataprocessor.model.performance.Profiler.profile(Profiler.java:15)
at sun.reflect.GeneratedMethodAccessor160.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:64)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:77)
at com.testProj.dataprocessor.common.sql.SqlRetryPolicy.retry(SqlRetryPolicy.java:20)
at sun.reflect.GeneratedMethodAccessor161.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:64)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
at com.testProj.dataprocessor.common.sql.SqlStore$$EnhancerByCGLIB$$f3a323cc.upsertDataHistory(<generated>)
at com.testProj.dataprocessor.dao.DataDAO.updateDataHistory(DataDAO.java:88)
at com.testProj.dataprocessor.eventhandler.DataHistoryEventHandler.doWork(DataHistoryEventHandler.java:34)
at com.testProj.dataprocessor.eventhandler.DataHistoryEventHandler.updateDiagnosticsHistory(DataHistoryEventHandler.java:28)
at com.testProj.dataprocessor.DataProcessorService.doWork(DataProcessorService.java:37)
at com.testProj.dataprocessor.DataProcessorService.process(DataProcessorService.java:24)
at com.testProj.dataprocessor.DataProcessorService.process(DataProcessorService.java:80)
at com.testProj.dataprocessor.DataProcessorService.postDataEventSync(DefaultDataProcessorService.java:41)
at com.testProj.dataprocessor.DataProcessorService.postDataEvent(DefaultDataProcessorService.java:36) at sun.reflect.GeneratedMethodAccessor272.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:173)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:89)
at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:60)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:75)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:236)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:109)
at Caused by: java.sql.SQLException: Violation of PRIMARY KEY constraint 'PK_DataHistory_on_DataHistoryGuid'. Cannot insert duplicate key in object 'dbo.DataHistory'. The duplicate key value is (0500edac-0074-4324-3436-31444231342d).
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:613)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:572)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:727)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2275) ... 68 more
Assumptions:
1.As Sql Server websites has mentioned that 1 in a billion Guid's created will be a duplicate, so i don't expect duplicates to be present in my tables.
Although the question is focussed on Hibernate, the following line in the trace indicates that the database is rejecting the insert.
Caused by: java.sql.SQLException: Violation of PRIMARY KEY constraint 'PK_DataHistory_on_DataHistoryGuid'. Cannot insert duplicate key in object 'dbo.DataHistory'. The duplicate key value is (0500edac-0074-4324-3436-31444231342d).
This is more of a design issue than a problem with SQL Server or Hibernate. If an auto generated UUID as a PRIMARY KEY in the DataHistory table is your goal,
then you are much better off using the database to achieve this, rather than using Hibernate (or worse, rolling your own solution).
This is because the database is designed to avoid a collision of values, if a UNIQUE INDEX or PRIMARY KEY is specified.
See SQL UNIQUE Constraint
The task of creating the auto generated UUID is available for you, with PRIMARY KEY DEFAULT (NEWID()).
If you need to use the UUID later, then use a non primary key value to select the value after it has been created.
Example table:
CREATE TABLE dbo.DataHistory
(
DatahistoryGuid uniqueidentifier PRIMARY KEY DEFAULT (NEWID()),
history_row integer NOT NULL,
data_stuff nvarchar(4000) NOT NULL,
created_date datetime2 DEFAULT (GETDATE())
);
Try setting the insertable property to false in Hibernate. Do this for all columns defaulted by the database. For example:
#Column(name = "DatahistoryGuid", insertable=false)
#Column(name = "created_date", insertable=false)
This results in SQL similar to the following:
INSERT INTO dbo.DataHistory (history_row, data_stuff)
VALUES (1234, 'Joe Bloggs');
See Related Question
To get the DatahistoryGuid value, use a SELECT statement:
SELECT DatahistoryGuid from dbo.DataHistory where history_row = 1234;
SQL Fiddle Example
Using apache derby ( 10.12.1.1), I created the following tables:
CREATE TABLE ROWCONTENT(
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY,
MD5SUM CHAR(32) UNIQUE,
CONTENT CLOB,
CONTIG VARCHAR(20),
START INT,
STOP INT,
REF VARCHAR(50) NOT NULL
);
CREATE TABLE VCF(
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY,
NAME VARCHAR(255)
);
CREATE TABLE VCFROW(
ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY,
VCF_ID INTEGER CONSTRAINT row2vcf REFERENCES VCF,
ROW_ID INTEGER CONSTRAINT row2content REFERENCES ROWCONTENT
);
but when I try a SELECT using the following statement
SELECT
VCF.ID,VCF.NAME, COUNT(VCFROW.ID) as "COUNT_VARIANTS"
FROM
VCF,VCFROW,ROWCONTENT
WHERE
VCFROW.VCF_ID=VCF.ID AND
VCFROW.ROW_ID = ROWCONTENT.ID AND
ROWCONTENT.CONTIG IS NOT NULL
GROUP BY VCF.ID
I get the following exception:
java.sql.SQLSyntaxErrorException: Column reference 'VCF.NAME' is invalid, or is part of an invalid expression. For a SELECT list with a GROUP BY, the columns and expressions being selected may only contain valid grouping expressions and valid aggregate expressions.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeQuery(Unknown Source)
Do you know why ?
Thanks.
First use explicit JOIN:
...
FROM VCF
JOIN VCFROW
ON VCFROW.VCF_ID=VCF.ID
JOIN ROWCONTENT
ON VCFROW.ROW_ID = ROWCONTENT.ID
WHERE ROWCONTENT.CONTIG IS NOT NULL
GROUP BY VCF.ID;
You could also add table aliases so you don't have to write entire table names.
Second VCF.NAME is not part of GROUP BY and it isn't wrapped with aggregation function.
Depending on needs use:
GROUP BY VCF.ID,VCF.NAME
-- or
SELECT VCF.ID, MAX(VCF.NAME) AS NAME, COUNT(VCFROW.ID) AS COUNT_VARIANTS
Related: Group by clause in mySQL and postgreSQL, why the error in postgreSQL?
In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause
I am accessing HSQLDB through A jdbc drive in my java program. The script file of the database is as follows.
CREATE SCHEMA PUBLIC AUTHORIZATION DBA
CREATE CACHED TABLE TBL1(ID CHAR(5),NAME VARCHAR(11),ZIP VARCHAR(11))
CREATE UNIQUE INDEX IDX_ID ON TBL1(ID)
CREATE CACHED TABLE TBL2(ID CHAR(5),STREET CHAR(5))
CREATE INDEX IDX_RS_ID ON TBL2(ID)
CREATE CACHED TABLE TBL3(ID VARCHAR(8) NOT NULL PRIMARY KEY,LON FLOAT,LAT FLOAT)
CREATE GLOBAL TEMPORARY MEMORY TABLE OVERLAY(ID CHAR(8),AROUTE CHAR(10),ZROUTE
CHAR(10),TYPE CHAR(8)) ON COMMIT PRESERVE ROWS
SET TABLE TBL1 INDEX'4490648 5643624 0'
SET TABLE TBL2 INDEX'529105280 529105280 0'
SET TABLE TBL3 INDEX'775814624 0'
CREATE VIEW OVERLAYSPAN (ID,STREET) AS SELECT DISTINCT ID, NAME FROM OVERLAY AS O, TBL2 as
R WHERE O.ID = R.ID
CREATE USER SA PASSWORD ""
GRANT DBA TO SA
SET WRITE_DELAY 10
In the java program, I am first removing all entries (1000 entries) from TBL1 and reinserting new entries (1500) in the same table. The program executes normally, but, when I am attempting to re-execute the same program, I am getting the following error.
java.sql.SQLException: error in script file line: 9 S1000 General error
java.lang.NullPointerException in statement [SET TABLE TBL2 INDEX'529105280 529105280 0']
Here is the fill stack trace from the program.
java.sql.SQLException: error in script file line: 9 S1000 General error java.lang.NullPointerException in statement [SET TABLE TBL2 INDEX'529105280 529105280 0']
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
at org.hsqldb.jdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at HSQLTesting.main(HSQLTesting.java:98)
Where at line 98, the program is attempting to make a connection with the database.
Connection sqlconnect = DriverManager.getConnection("jdbc:hsqldb:file:C:/birdseyedb/birdseye", "SA", "");
Any idea what's going wrong here?
Thanks in advance.
Hi i have a Problem while inserting data in my databases ...
String query = "INSERT INTO gutscheine (code, Wert) values ("+Code+","+Wert+")";
SQLConnection sql = new SQLConnection("Gutscheine.db");
PreparedStatement ps;
try {
ps = sql.con.prepareStatement(query);
int i = ps.executeUpdate();
if (i == -1) {
System.out.println("db error : " + query);
}
} catch (SQLException e) {
e.printStackTrace();
}
sql.CloseConnection(); // my own funktion
My Error:
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: GUTSCHEINE
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at com.daniel.guthaben.Gutschein.<init>(Gutschein.java:33)
at com.daniel.guthaben.Main.main(Main.java:7)
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: GUTSCHEINE
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.SchemaManager.getTable(Unknown Source)
at org.hsqldb.ParserDQL.readTableName(Unknown Source)
at org.hsqldb.ParserDQL.readRangeVariableForDataChange(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 4 more
Here i am trying adding context from the root user ... (on the left you can see the table exists)
http://i.stack.imgur.com/FS5ws.png
Sorry I was using diffrent databases ... :(
it's generally not advised to use string building for your query. String query = "INSERT INTO gutscheine (code, Wert) values (?, ?)"; and then running it through a safe query preparation is going to be better.
That said, the error sounds pretty obvious? user lacks privilege or object not found: GUTSCHEINE. Doesn't look like you're connecting as a particular user from your code, so I suspect you forgot to pass along your username and password when setting up the database connection. If that part's fine, verify Gutscheine.db exists.
The problem here is simple. When you are inserting string values, you need to enclose them in single quotes.
For example, "insert into table(col1, col2) values ('ABC','xyz');"
Have a look at the example provided in this link:
https://www.tutorialspoint.com/hsqldb/hsqldb_insert_query.htm
In your case, you need to enclose the values of Code and Wert in single quotes before you insert, or at the time of insert.
The following approach worked for me:
Code = "'" + Code + "'";
Wert = "'" + Wert + "'";
Use your insert query now, and it should work.
Even though hsql is quite forgiving and requires no infrastructure, you need to create the table upfront - e.g.
CREATE TABLE Gutscheine ( code varchar 20, Wert varchar 20 )
Also, you might want to read about SQL-Injection and use PreparedStatement. While you're at it, google for "little bobby tables"
I haven't run into privilege issues with hsql yet, but you might want to check how you open the connection, e.g. if you give a proper user account. You can actually check a proper one by looking at the hsql storage, which is clear text. It should contain the required user name - e.g. "SA"
Also, with that error message on google, several threads come up that suggest that hsql version upgrade might help, or that you ended up with a corrupt database after not executing "SHUTDOWN;" after creation of the database. Another one is the database name "null" in your .script file. Well, check them for yourself and try which solution matches your problem (the two linked threads point to several possible solutions):
http://sourceforge.net/p/hsqldb/mailman/message/28944633/
http://www.coderanch.com/t/460394/JDBC/databases/HSQLDB-user-lacks-privilege-object
I want to load SQL script files for my unit tests. As I am using Spring 2.5.2, I decided to use the SimpleJdbcTestUtils.executeScript() method to load my script file, using the following code:
DriverManagerDataSource dataSource = ... // getting my DataSource defined in my Spring context
SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);
Resource resource = new ClassPathResource("/create-table.sql");
SimpleJdbcTestUtils.executeSqlScript(template, resource, true);
If I write each SQL statement in one line in the create-table.sql file, then everything is ok. But if I write a statement on multiple lines, then I get an error, even if the statement is finished by a ;.
Working script :
CREATE TABLE T_FOO (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP, IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);
CREATE TABLE T_BAR (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP, IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);
Not working script:
CREATE TABLE T_FOO (
ID NUMERIC PRIMARY KEY,
DATEID TIMESTAMP,
IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);
CREATE TABLE T_BAR (
ID NUMERIC PRIMARY KEY,
DATEID TIMESTAMP,
IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);
Error on second file:
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [CREATE TABLE T_FOO (]; nested exception is java.sql.SQLException: Unexpected token: in statement [CREATE TABLE T_FOO (]
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:111)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:322)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:404)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:519)
at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.update(SimpleJdbcTemplate.java:237)
at org.springframework.test.jdbc.SimpleJdbcTestUtils.executeSqlScript(SimpleJdbcTestUtils.java:150)
at org.springframework.test.jdbc.SimpleJdbcTestUtils.executeSqlScript(SimpleJdbcTestUtils.java:113)
at foo.bar.HsqldbUtils.run(HsqldbUtils.java:95)
at foo.bar.SomeUnitTest.executeTests(SomeUnitTest.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: Unexpected token: in statement [CREATE TABLE T_FOO (]
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.jdbcStatement.executeUpdate(Unknown Source)
at org.springframework.jdbc.core.JdbcTemplate$1UpdateStatementCallback.doInStatement(JdbcTemplate.java:509)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:393)
As I am willing to create more complex tables, I prefer to write the SQL instruction in a more readable way, so using several lines. Is there a way to do that?
You're using Spring 2.5.2, but referring to the 2.5.6 documentation.
If you look at the javadoc for 2.5.2 (see here), you'll note that it doesn't support multi-line scripts.
This was apparently resolved in 2.5.4 (issue here).
You should upgrade to 2.5.6, or better yet, 3.0.x.