javamysqlhibernatehibernate-mappingschemaexport

Getting unknown database exception while creating schema programmatically?


I am trying to create schema and tables programmatically using hibernate/GWT/Java. I created the configuration with all mapping resources and properties etc. follows,

Configuration hibConfiguration = new Configuration().configure(configFileDoc);

but when I say

SchemaExport schemaExport = new SchemaExport(hibConfiguration);
schemaExport.create(true, true);

it is throwing exception as,

ERROR: HHH000231: Schema export unsuccessful

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'testschema'
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.Util.getInstance(Util.java:386)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
        at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
        at sun.reflect.GeneratedConstructorAccessor36.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:154)
        at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:193)
        at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:55)
        at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
        at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
        at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
        at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)

my cfg.xml file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.password">passwd</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testSchema</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
      <property name="javax.persistence.validation.mode">none</property>
      <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
      <property name="hibernate.default_entity_mode">dynamic-map</property>
      <property name="hibernate.hbm2ddl.auto">create</property>
   </session-factory>
</hibernate-configuration>

And If I manually created the schema then it is working fine, i.e. creating tables and columns also. But normally it is not creating schema, Is it not possible to create schema automatically?


Solution

  • Hibernate will not create the schema dynamically...you have to manually create it. I used simple JDBC connection and statement to create schema, then SchemaExport will do the work of generating the tables and fields. So there is no way to create actual schema using hibernate. You have to create it manually.

     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = factory.newDocumentBuilder();
     Document configFileDoc = builder.parse(new ByteArrayInputStream(configFileStr.getBytes()));
    
     Configuration hibConfiguration = new Configuration();
     hibConfiguration.configure(configFileDoc);
     ArrayList<Document> hbmFileDocs = getHBMFileDocList(); //created the documents of the file string
     for(Document doc : hbmFileDocs)
            hibConfiguration.addDocument(doc);
    
     hibConfiguration.buildMappings();
     Class.forName("com.mysql.jdbc.Driver");
     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "username", "password");
     stmt = conn.createStatement();
     String sql = "CREATE DATABASE "+schemaName;
     stmt.executeUpdate(sql);
     SchemaExport schemaExport = new SchemaExport(hibConfiguration);
     schemaExport.create(true, true);
    

    This will generate the complete schema, it worked for me.