How to install database with Spring MVC
Question: I've got a Spring MVC project which I've received from another developer. I
usually find .sql files there to initialize the DB, but this code doesn't seem
to have anything like that. I doesn't use Roo or any tools that I know that
can do database initialization.

I've been using Python/Django for a while now, so I guess my question is: is
there something I could use with Spring MVC like Django's "manage.py syncdb"
to initialize the database based on the code? Maybe a build task or a script
or anything other than reverse engineering the code and figuring out what
tables should go to the DB?

Thanks!


Answer: ## The easiest solution

  * If you can still talk to that "other developer" => **just ask**

If the above does not work...

You have to understand what persistence mechanism / technology this project is
based on. Here are few examples:

## It Is Based On Hibernate

Just search for the word `hibernate` in all the files. Do you see anything?
Take a look at project's dependencies, does it have any hibernate jars? etc..

If it is indeed Hibernate, you can generate your schema based on your domain
objects (close to Django's "manage.py syncdb"), by adding the following
Hibernate property:

    
    
    <prop key="hibernate.hbm2ddl.auto">create</prop>
    

to a sessionFactory bean. It would look similar to
[this](https://github.com/anatoly-polinsky/money-making-
project/blob/master/src/main/resources/META-INF/conf/spring/persistence-
spring-config.xml) configuration. Or if you have "persistence.xml" file
instead, add this property ( under Hibernate properties ) there. You'd have to
"run" the project in order to create this schema.

## It Is Based On Spring JDBC
([Template](http://static.springsource.org/spring/docs/3.1.x/javadoc-
api/org/springframework/jdbc/core/JdbcTemplate.html))

Again, search for it.

In this case you will have to write some SQL ( think about it as Rails
migration, but in SQL :) ). Once you do that, you can use _in memory_ DB, such
as [H2](http://www.h2database.com/html/main.html) (by default it uses HSQLDB),
to actually achieve the same effect as with "hibernate.hbm2ddl.auto":

    
    
    <jdbc:embedded-database id="dataSource">
        <jdbc:script location="classpath:schema.sql"/>
        <jdbc:script location="classpath:test-data.sql"/>
    </jdbc:embedded-database>
    

Here is more about [Embedded DB
Support](http://static.springsource.org/spring/docs/3.1.x/spring-framework-
reference/html/jdbc.html#jdbc-embedded-database-support)

## It Is Based On Magic

Nothing to search for really in this case (well, maybe for `DAO` or
`Repository`), but the simplest approach would be to just take a single
service, and follow the white rabbit:

    
    
    service.rememberThePlace( place ) { placeDao.save( place ) }
    

Now look at the Dao.. does it have any `Hibernate` imports? What about
`...JdbcTemplate`, etc..

