Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

You need to add one more property in your hibernate config file, to create the database/schema automatically:

<property name="hibernate.hbm2ddl.auto">create</property>

I guess hibernate create schema only in case of database is created. You need to create database testschema manually.

Create database testschema manually and then start your hibernate application.

It is possible to create schemas automatically. Add the following property to your Hibernate config xml:

<property name="hibernate.hbm2ddl.auto">update</property>

OR

<prop key="hibernate.hbm2ddl.auto">update</prop>

As per the above property, Hibernate will only create the database if it doesn't already exist, and will update any existing tables to match your current domain model.

Another choice will be this:

 <property name="hibernate.hbm2ddl.auto">create-drop</property>

The above property implies that Hibernate will create a brand-new database each time you start the application

UPDATE: USEFUL URLs

http://manikandanmv.wordpress.com/2011/04/13/hibernate-basics-simple-example/

http://khartig.wordpress.com/2008/12/20/dynamic-schema-and-database-creation-with-netbeans-hibernate-and-ejb3/

http://blog.sencide.com/2011/03/hibernate-tutorial-for-beginners.html

http://www.jroller.com/eyallupu/entry/hibernate_s_hbm2ddl_tool

http://hannelita.wordpress.com/2012/03/21/hibernate-cfg-hibernate-4-0-0/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top