我有下面的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-一切正常。
3条答案
按热度按时间aor9mmx11#
当我使用Spring的时候,我通常使用属性文件来自动创建一个数据库,下面是它是如何完成的,希望这能起作用,这样你就可以修改它来满足你的需要.....
92dk7w1h2#
MySQL将在数据库中创建模式,但不会为您创建实际的数据库。在Hibernate中运行模式导出之前,您的本地MySQL安装中必须有一个名为“userdb”的现有数据库(登录并运行类似于“create database userdb”的命令)。
4dbbbstv3#
我的想法是你的MySQL没有一个数据库的模式。所以,至少你应该去检查它是否存在。我使用MySQL工作台(相当不错的工具),所以只要检查/创建它,如果它不存在,然后再试一次。