0

i am writing spring mvc project. i wanna to create mysql database at startUp of my application. tried to read file "import.sql". And i got an error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'test3'

here is my spring-servlet.xml:

    <context:annotation-config />
<context:component-scan base-package="com.joseph" />    


<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
    <value>
        hibernate.dialect=${hibernate.dialect}
        hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size}
        hibernate.show_sql=${hibernate.show_sql}
        hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
        hibernate.id.new_generator_mappings=${hibernate.id.new_generator_mappings}
        hibernate.hbm2ddl.import_files=${hibernate.hbm2ddl.import_files}

     </value>
</property>

</bean>


<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
<tx:annotation-driven />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean> 

and jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost:3306/test3  
jdbc.username=root
jdbc.password=root

    #org.hibernate.dialect.PostgreSQLDialect
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    hibernate.show_sql = true
    hibernate.id.new_generator_mappings = true
    hibernate.hbm2ddl.auto = create
    hibernate.jdbc.batch_size = 5
    hibernate.transaction.factory_class=org.transaction.JDBCTransactionFactory
    hibernate.hbm2ddl.import_files = import.sql 

import.sql

CREATE SCHEMA IF NOT EXISTS test3;

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>   
     <mapping class="com.joseph.model.Student" />        
    </session-factory>         
</hibernate-configuration>
titusn
  • 1,201
  • 1
  • 12
  • 43
lev
  • 1
  • 1
  • Because no database with `test3` does not exist. you should create a database (schema) named `test3` first, then during application startup, your tables will be created. – Ali Dehghani Dec 30 '14 at 10:30
  • that's it. How can i automatically create schema at application startup ? – lev Dec 30 '14 at 10:40
  • It's not a good idea to generate schema (and even tables) automatically. See [Hibernate/JPA DB Schema Generation Best Practices](http://stackoverflow.com/questions/2585641/hibernate-jpa-db-schema-generation-best-practices) – Ali Dehghani Dec 30 '14 at 10:50
  • I know, but anyhow that is exactly my task. – lev Dec 30 '14 at 10:54

1 Answers1

1

Add a import.sql file in resource as follow:

/*create database at first time*/ 
CREATE SCHEMA your-database-name;

'hibernate.cfg.xml' as follow:

<hibernate-configuration>
    <session-factory>
       ...
       ...
       <property name="hbm2ddl.import_files">import.sql</property>
       ...
       ...
    </session-factory>
</hibernate-configuration>

Also refer this for doing this programmatically

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • 1
    problem of this solution that hibernate first trying to connect to the database(scheme) and only after that goes to import.sql and creates a new scheme(according to the file context). How to make order opposite? – lev Dec 30 '14 at 16:47