为什么Hibernate没有为MySQL创建数据库?

我有以下hibernate.cfg.xml:

    jdbc:mysql://localhost:3306/userdb root 1234 com.mysql.jdbc.Driver org.hibernate.dialect.MySQL5Dialect true true create 1 thread    

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

pom.xml中:

 ...   org.hibernate hibernate-core 4.0.1.Final   org.hibernate hibernate-validator 4.2.0.Final   org.hibernate.common hibernate-commons-annotations 4.0.1.Final tests   org.hibernate.javax.persistence hibernate-jpa-2.0-api 1.0.1.Final   org.hibernate hibernate-entitymanager 4.0.1.Final   javax.validation validation-api 1.0.0.GA provided   mysql mysql-connector-java 5.1.27    

调用以下代码行时:

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

我看到以下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.(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.(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.(HibernateUtil.java:12) at com.beingjavaguys.hbn.App.saveUser(App.java:45) at com.beingjavaguys.hbn.App.main(App.java:30) 

这个问题的原因是什么?

怎么解决?

PS

MySql中不存在数据库模式!

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

是从Java应用程序创建模式的方法吗?

我通常使用属性文件在我使用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 

MySQL将在数据库中创建您的模式,但不会为您创建实际的数据库。 在hibernate中运行模式导出之前,您必须在本地MySQL安装中使用名为“userdb”的现有数据库(登录并运行类似“create database userdb”)。

我的想法是你的MySQL没有数据库架构。 所以,至少你应该检查它是否存在。 我使用MySQL Workbench(相当不错的工具),所以只要它不存在就检查/创建它,然后再试一次。