小编典典

Hibernate为什么不为MySQL创建数据库?

java

我有以下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>

我尝试了另一种方言(org.hibernate.dialect.MySQLDialect),但结果却很旧

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>

当调用以下代码行时:

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

我看到以下堆栈跟踪:

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)

这个问题的原因是什么?

如何解决?

聚苯乙烯

数据库架构在MySql中不存在!

如果我明确添加数据库shema-一切正常。

从Java应用程序创建架构的方法在哪里?


阅读 223

收藏
2020-11-26

共1个答案

小编典典

我通常在使用Spring时使用属性文件自动创建数据库,以下是它的完成方式,希望它能起作用,因此您将对其进行修改以适合您的需求.....

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
2020-11-26