Sunday, April 19, 2015

Error during statement execution (file: '/import.sql')

When I create a Maven project using artifact ID jboss-javaee6-webapp-archetype version 7.1.3.CR8, there are a bunch code are already crated for you. My personal habit is whenever a project is first created, I will make a deploy to ensure my server is up and running properly. Somehow I got the following message complaining something was went wrong:
11:30:50,752 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-8) HHH000231: Schema export unsuccessful: org.hibernate.tool.hbm2ddl.ImportScriptException: Error during statement execution (file: '/import.sql'): insert into Member (id, name, email, phone_number) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212')
 at org.hibernate.tool.hbm2ddl.SchemaExport.importScript(SchemaExport.java:452) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:379) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:305) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:294) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:452) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
 at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
 at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) [rt.jar:1.6.0_34]
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.6.0_34]
 at java.lang.Thread.run(Thread.java:701) [rt.jar:1.6.0_34]
Caused by: org.h2.jdbc.JdbcSQLException: Table "MEMBER" not found; SQL statement:
insert into Member (id, name, email, phone_number) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212') [42102-161]
 at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
 at org.h2.message.DbException.get(DbException.java:169)
 at org.h2.message.DbException.get(DbException.java:146)
 at org.h2.command.Parser.readTableOrView(Parser.java:4749)
 at org.h2.command.Parser.readTableOrView(Parser.java:4727)
 at org.h2.command.Parser.parseInsert(Parser.java:949)
 at org.h2.command.Parser.parsePrepared(Parser.java:375)
 at org.h2.command.Parser.parse(Parser.java:279)
 at org.h2.command.Parser.parse(Parser.java:251)
 at org.h2.command.Parser.prepareCommand(Parser.java:217)
 at org.h2.engine.Session.prepareLocal(Session.java:415)
 at org.h2.engine.Session.prepareCommand(Session.java:364)
 at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1119)
 at org.h2.jdbc.JdbcStatement.executeUpdateInternal(JdbcStatement.java:121)
 at org.h2.jdbc.JdbcStatement.executeUpdate(JdbcStatement.java:110)
 at org.jboss.jca.adapters.jdbc.WrappedStatement.executeUpdate(WrappedStatement.java:371)
 at org.hibernate.tool.hbm2ddl.DatabaseExporter.export(DatabaseExporter.java:64) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 at org.hibernate.tool.hbm2ddl.SchemaExport.importScript(SchemaExport.java:447) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
 ... 16 more
I was wondering since when I got such an import.sql get invoked in my code? I was searching in the whole project code and I didn't find any. Later I found out this blog, Hibernate has a mechanism to load SQL script on startup.
You can execute an SQL script during the SessionFactory creation right after the database schema generation to import data in a fresh database. You just need to add a file named import.sql in your classpath root and set either create or create-dropas your hibernate.hbm2ddl.auto property.
Got it, that file did really exists in the classpath. To make my life simple, I would just rename import.sql to other name.

No comments: