6

I have following hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <property name="connection.url">jdbc:mysql://localhost:3306/userdb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <property name="show_sql">true</property>

        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="connection.pool_size">1</property>
        <property name="current_session_context_class">thread</property>

        <mapping class="com.beingjavaguys.hbn.User" />

    </session-factory>
</hibernate-configuration>

I tryed another dialect(org.hibernate.dialect.MySQLDialect) but I see old result

pom.xml:

...
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>4.0.1.Final</version>
            <classifier>tests</classifier>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>
        </dependencies>
</project>

When invokes following code line:

    return new Configuration().configure().buildSessionFactory();

I see following stacktrace:

ERROR: HHH000231: Schema export unsuccessful
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'userdb'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ...
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
    at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:368)
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:305)
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:294)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:452)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at com.beingjavaguys.hbn.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
    at com.beingjavaguys.hbn.HibernateUtil.<clinit>(HibernateUtil.java:12)
    at com.beingjavaguys.hbn.App.saveUser(App.java:45)
    at com.beingjavaguys.hbn.App.main(App.java:30)

What the reason of this problem?

How to fix it?

P.S.

database schema doesn't exist in MySql!

if I add Database shema explicitly - all works good.

Is where way to create schema from java application?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • usefull link for you http://stackoverflow.com/questions/16999259/force-hibernate-to-create-tables-after-droping-and-recreating-database – Nadendla Jul 08 '14 at 13:28

3 Answers3

14

I usually use the properties file to automatically create a database when i'm using Spring, and below is how its done, hope this works so u'll modify this to suite your needs.....

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/userdb?createDatabaseIfNotExist=true
database.user=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
Aph1ka
  • 368
  • 1
  • 4
  • 17
2

MySQL will create your schema within a database, but will not create your actual database for you. You must have an existing database called 'userdb' in your local MySQL installation (log in and run something like 'create database userdb') before you run the schema export in hibernate.

Matt
  • 3,303
  • 5
  • 31
  • 53
  • really? Is where way to create schema from java application? – gstackoverflow Jul 08 '14 at 13:26
  • Yes, hibernate will create the schema (it will create all your tables for you). But your connection to mysql requires a database, and that database has to exist already. I am not sure off the top of my head how to create databases form Java (try Google) - you could maybe do something hacky like connect to a different database, and run the create database script, then disconnect and connect to your new one, but that sounds pretty ugly – Matt Jul 08 '14 at 13:31
0

My thoughts are that your MySQL doesn't have a schema for your database. So, at least you should go to check if it exists. I use MySQL Workbench (quite nice tool), so just check/create it there if it doesn't exist and try again.

Rufi
  • 2,529
  • 1
  • 20
  • 41