2017-04-14 73 views
0

我正在使用spring,hibernate和mysql創建一個應用程序。 我在設置hibernate.hbm2ddl.auto =更新,我想這會強制mysql創建一個數據庫/表,如果不存在。但仍然由於某些原因,hibernate不會創建數據庫並拋出異常。請幫忙 我已經使用過各種方言請看下面但都是徒勞的。com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知數據庫'xyz'。 hbm2ddl.auto =更新不起作用

hibernate.properties文件

#JDBC Properties 
jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/emusicstoredb 
jdbc.username=root 
[email protected] 

#Hibernate Properties 
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 
hibernate.show_sql=true 
hibernate.hbm2ddl.auto=update 
hibernate.format_sql=true  

的applicationContext.xml文件

<context:property-placeholder location="/WEB-INF/hibernate.properties" /> 
    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${jdbc.driverClassName}" /> 
     <property name="url" value="${jdbc.url}" /> 
     <property name="username" value="${jdbc.username}" /> 
     <property name="password" value="${jdbc.password}" /> 
    </bean> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
       <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
      </props> 
     </property> 
     <property name="packagesToScan"> 
      <list> 
       <value>com.emusicstore</value> 
      </list> 
     </property> 
    </bean> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
     <property name="maxUploadSize" value="1024000" /> 
    </bean> 
</beans> 

控制檯

INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final} 
Apr 14, 2017 4:42:25 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.3.1.Final} 
Apr 14, 2017 4:42:25 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Apr 14, 2017 4:42:25 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Apr 14, 2017 4:42:26 PM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure 
WARN: HHH000342: Could not obtain connection to query metadata : Unknown database 'emusicstoredb' 
Apr 14, 2017 4:42:26 PM org.hibernate.dialect.Dialect <init> 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 
Apr 14, 2017 4:42:26 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation 
INFO: HHH000422: Disabling contextual LOB creation as connection was null 
Apr 14, 2017 4:42:27 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService 
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 
Apr 14, 2017 4:42:27 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> 
INFO: HHH000397: Using ASTQueryTranslatorFactory 
Apr 14, 2017 4:42:28 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute 
INFO: HHH000228: Running hbm2ddl schema update 
Apr 14, 2017 4:42:28 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute 
INFO: HHH000102: Fetching database metadata 
Apr 14, 2017 4:42:28 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute 
ERROR: HHH000319: Could not get database metadata 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'emusicstoredb' 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) 
    at com.mysql.jdbc.Util.getInstance(Util.java:381) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910) 
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923) 
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031) 
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718) 
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) 
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153) 
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144) 
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155) 
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120) 
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139) 
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51) 
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194) 
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:521) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454) 
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:743) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>mywebsite</groupId> 
    <artifactId>emusicstore</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>emusicstore Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> 

     <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 

     <!-- https://mvnrepository.com/artifact/jstl/jstl --> 
     <dependency> 
      <groupId>jstl</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/jstl/jstl --> 
     <dependency> 
      <groupId>taglibs</groupId> 
      <artifactId>standard</artifactId> 
      <version>1.1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId>commons-fileupload</artifactId> 
      <version>1.2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-io</groupId> 
      <artifactId>commons-io</artifactId> 
      <version>2.4</version> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.6</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.3.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.0-api</artifactId> 
      <version>1.0.0.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 

    </dependencies> 
    <build> 
     <finalName>emusicStore</finalName> 
    </build> 
</project> 

請注意: 我已經使用了以下方言:

org.hibernate.dialect.MySQL5Dialect

org.hibernate.dialect.MySQLInnoDBDialect

org.hibernate.dialect.MySQLDialect

我也創建了一個數據庫,然後拋出異常的表而不是數據庫。

而且已經做了maven更新,乾淨的項目,刪除.m2文件夾,浪費了4個小時,但都是徒勞的。 請幫忙。

+0

據我所知,和我們一樣,手動創建數據庫並添加到調度程序servlet稍後將在更新期間通過休眠創建表名稱。而在CREATE db的情況下也將創建 – Hema

+0

我創建了一個數據庫並且然後運行該應用程序。它確實創建了表格。所以現在有更新的方法來創建一個數據庫,如果不存在? –

+0

雖然使用更新創建數據庫不會發生......你會得到同樣的錯誤,因爲你提到 – Hema

回答

1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'emusicstoredb' 

此異常是因爲應用中沒有找到數據庫名稱 - >emusicstoredb 休眠的hbm2ddl.auto財產或者創建,更新或驗證數據庫表。這不是創建數據庫的責任,您應該先創建數據庫名稱emusicstoredb,然後運行您的應用程序。

+0

謝謝,我的印象是,它可以創建一個數據庫。在這裏浪費了很多時間。無論如何感謝您清除這個 –

+0

如果這有助於你,你可以接受這個答案,因此它可以幫助另一個人。 –

+0

但是我只想澄清一件事:爲了使這項工作成爲可能,我必須在applicationContext.xml中添加**:「annotatedClasses」**以及**「packagesToScan」**,但我不確定爲什麼packagesToScan單獨運行沒有工作? –

相關問題