简体   繁体   中英

SQL syntax error initializing DB before unit Tests

I have created a .sql for preparing my DB for testing.

here a part of the .sql which produces the error:

DROP TABLE IF EXISTS announcement;
CREATE TABLE announcement (
   id int(11) NOT NULL AUTO_INCREMENT,
   date datetime DEFAULT NULL,
   title varchar(255) DEFAULT NULL,
   content mediumtext,
   PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

LOCK TABLES announcement WRITE;
INSERT INTO announcement VALUES (1,'2015-07-29 10:59:16','Test Anno ','some text');
UNLOCK TABLES;

when executed in Mysql Workbench this scipt works fine.

however when executed via hibernate:

String sqlScript = readFile("dump.sql", Charset.forName("UTF8"));
//System.err.println(sqlScript);
Query q = em.createNativeQuery("BEGIN " + sqlScript + "END;");
q.executeUpdate();

I get:

2015-08-06 16:15:55 ERROR SqlExceptionHelper:146 - 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 'DROP TABLE IF EXISTS announcement;
CREATE TABLE announcement (
  id int(11) NO' at line 1

I am using:

Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
jpaProperties.put("hibernate.enable_lazy_load_no_trans", true);

Can someone help me out here? thanks.

As stated by mustaccio BEGIN and END statements are not allowed outside of stored procedueres. See: Hibernate multiple native SQL statements

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